Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generating random numbers 1 to 30 and activating 10 of them

Discussion in 'Scripting' started by JPF55, Sep 10, 2015.

  1. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Hello,
    I've been trying to script a code so it will randomly generate a number from 1 to 30, then checks to see if that number has been generated before, if so generates a new number and checks it, repeats until 10 numbers are generated, then activates 10 objects that are associated with the numbers, so 1 will activate gameObject One, and 30 will activate gameObject Thirty, so on and so on.

    Does anyone have any ideas about how to do this?

    I know that var number = Random.Range(1,30); should get my numbers and I could use a bunch of if statements but that would get to big too fast.
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Many simple ways to achieve this:

    Store activated values in a list and check with list.contains(), have a an array of 30 bool that you set to true for activated values and check if (activatedValue[value] == false) etc...
     
  3. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    how do I put the numbers in the list and check them?
     
    Last edited: Sep 10, 2015
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    using System.Collections.generic;

    public List<int> numberLsit = new List<int>();

    void Start()
    {
    for(int i = 0; i < 10; i++)
    {
    int randomNum = Random.range(0,30);
    if(numberList.Contains(randomNum))
    {
    i -= 1;
    }
    else
    {
    numberList.Add(randomNum);
    }
    }
    }

    thats one way. hope it helps. I havnt tries the code. the i-1 might not work. it might cause an infinite loop or something but it has the basic jist of what you want.
     
  5. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    That should work great for getting the numbers, is there anyway to put gameObjects into a list?
     
  6. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    ok you can either create another list of gameObjects and their index will correspond to the index of the int list as long as the list is never altered.

    you can make a list of gameObjects the same way
    public List<GameObject> objects = new List<gameObject>();

    void Start()
    {
    objects.Add(whateverGameObjectYouWant);
    }

    or it will be better to create a class that contains both a gameObject and an int and creating a list of that class.
     
  7. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    public class gameObjectWithInID:
    {
    public Int gameObjectID;
    publci GameObject gameObject;

    public gameObjectWithID(int id,GameObject object)
    {
    gameObjectId = id;
    gameObject = object;
    }

    }

    public class yourScript: MonoBehaviour
    {

    void Start()
    {
    //you can then use a loop to generate the class above the same way i showed earlier
    }
    }
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    I'd just generate a list of all of the values you're going to pick from, shuffle that list, and pick the first 10 values.

    The Knuth shuffle is a common shuffle algorithm.
     
  9. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    The code works fine for generating the numbers, but now I am having a problem activating the objects, it makes the 10 numbers but it is only activating 5 sometimes not even 5 get activated
     
    Last edited: Sep 11, 2015
  10. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    can you elaborate on what you mean by activate. do you mean setActive?
     
  11. like_a_bosss

    like_a_bosss

    Joined:
    Mar 18, 2015
    Posts:
    56
    took like 1.5 hour
    Code (JavaScript):
    1. #pragma strict
    2. var NumberOfNeededRondomNo:int;
    3. var ArrayOfNumbers:int[];
    4. var gameObjects:GameObject[]; /*<--------------------should  be as  much as  in  NumberOfNeededRondomNo field*/
    5. var numberOFobjecttoACTIVATE:int;
    6. private var randomNO:int;
    7. private var Is_the_number_in_the_array:int;
    8. private var ToGetAllRandomNo:int;
    9.  
    10.  
    11. function Start(){
    12. ToGetAllRandomNo=NumberOfNeededRondomNo*6;
    13.    
    14.    
    15.    
    16.     ArrayOfNumbers+=[randomNO];
    17.     storeNUMBERSinArray();
    18.     yield WaitForSeconds(2);
    19.     dezactivate_all_activate_number_of_them();
    20. }
    21.  
    22. function Update(){
    23.  
    24.  
    25. }
    26.  
    27. function storeNUMBERSinArray(){
    28. var b:int;
    29. var i:int;
    30.  
    31.     for(i=0;i<ToGetAllRandomNo;i++){randomNO=Random.Range(0,NumberOfNeededRondomNo);/*generates random number   to be check*/
    32.    
    33.                 if(ArrayOfNumbers.Length< NumberOfNeededRondomNo){/*check the current  length of the arrey and if its shorterthen goes farther*/
    34.                 Is_the_number_in_the_array=ArrayOfNumbers.IndexOf(ArrayOfNumbers,randomNO);/*checks if  generated number is already in array,if generated random numer is not in array then Indexof returns -1*/
    35.                  
    36.                   if(Is_the_number_in_the_array<0){ArrayOfNumbers+=[randomNO];}/*if<0==ArraOfNumbers.IndexOf(ArraOfNumbers,randomNO)=-1 .then adds randomNO to array*/
    37.                                                                
    38.                                                                
    39.                                                                
    40.                                                                
    41.                                                                  }}}
    42.                                                                
    43.                                                                
    44. function dezactivate_all_activate_number_of_them(){
    45. var i:int=0;
    46. for(i=0;i<gameObjects.Length;i++){
    47. gameObjects[i].active=false;
    48. }
    49. for(i=0;i<numberOFobjecttoACTIVATE;i++){
    50. gameObjects[i].active=true;
    51. }
    52.  
    53.  
    54.  
    55. }                                                            
    56.                                                                
    57.                                                                
    58.                                                                  
     
  12. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Code (CSharp):
    1.  List<int> GetRandomObjects(int total, int nbObjects)
    2.     {
    3.         List<int> allObjects = new List<int>();
    4.         List<int> retObjects = new List<int>();
    5.  
    6.         for (int i = 0; i < total; i++)
    7.         {
    8.             allObjects.Add(i);
    9.         }
    10.  
    11.         for (int i = 0; i < nbObjects; i++)
    12.         {
    13.             int rndId = Random.Range(0, int.MaxValue) % allObjects.Count;
    14.  
    15.             retObjects.Add(allObjects[rndId]);
    16.  
    17.             allObjects.RemoveAt(rndId);
    18.         }
    19.  
    20.         return retObjects;
    21.     }