Generating random numbers 1 to 30 and activating 10 of them

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.

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…

how do I put the numbers in the list and check them?

using System.Collections.generic;

public List numberLsit = new List();

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.

That should work great for getting the numbers, is there anyway to put gameObjects into a list?

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 objects = new List();

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.

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
}
}

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.

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

can you elaborate on what you mean by activate. do you mean setActive?

took like 1.5 hour

#pragma strict
var NumberOfNeededRondomNo:int;
var ArrayOfNumbers:int[];
var gameObjects:GameObject[]; /*<--------------------should  be as  much as  in  NumberOfNeededRondomNo field*/
var numberOFobjecttoACTIVATE:int;
private var randomNO:int;
private var Is_the_number_in_the_array:int;
private var ToGetAllRandomNo:int;


function Start(){
ToGetAllRandomNo=NumberOfNeededRondomNo*6;
   
   
   
    ArrayOfNumbers+=[randomNO];
    storeNUMBERSinArray();
    yield WaitForSeconds(2);
    dezactivate_all_activate_number_of_them();
}

function Update(){


}

function storeNUMBERSinArray(){
var b:int;
var i:int;

    for(i=0;i<ToGetAllRandomNo;i++){randomNO=Random.Range(0,NumberOfNeededRondomNo);/*generates random number   to be check*/
   
                if(ArrayOfNumbers.Length< NumberOfNeededRondomNo){/*check the current  length of the arrey and if its shorterthen goes farther*/
                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*/
                 
                  if(Is_the_number_in_the_array<0){ArrayOfNumbers+=[randomNO];}/*if<0==ArraOfNumbers.IndexOf(ArraOfNumbers,randomNO)=-1 .then adds randomNO to array*/
                                                                
                                                                
                                                                
                                                                
                                                                 }}}
                                                                
                                                                
function dezactivate_all_activate_number_of_them(){
var i:int=0;
for(i=0;i<gameObjects.Length;i++){
gameObjects[i].active=false;
}
for(i=0;i<numberOFobjecttoACTIVATE;i++){
gameObjects[i].active=true;
}



}
 List<int> GetRandomObjects(int total, int nbObjects)
    {
        List<int> allObjects = new List<int>();
        List<int> retObjects = new List<int>();

        for (int i = 0; i < total; i++)
        {
            allObjects.Add(i);
        }

        for (int i = 0; i < nbObjects; i++)
        {
            int rndId = Random.Range(0, int.MaxValue) % allObjects.Count;

            retObjects.Add(allObjects[rndId]);

            allObjects.RemoveAt(rndId);
        }

        return retObjects;
    }