# Randomly Pick Between Two Numbers

Is it possible to randomly pick between numbers? Im not looking for a range between them. I simply want to randomly pick between two floating integers, like if I want to have it choose to pick the number 1 or the number 5. Is this possible?

Here’s how you do that:

make an array (or perhaps list) of the things in question

``````var choices:int[] = [ 1, 5, 78, 42, 3 ];
``````

now get the length of the array

``````var howManyThings:int = choices.length;
``````

now get a random number in that amount

don’t forget you want an index, not an ordinal, since it’s an array.

``````var myRandomIndex:int;
myRandomIndex = Random.Range( 0, howManyThings );
``````

(if you are not very familiar with Random.Range and the range it gives, read the documentation carefully.)

In our example this will be one of the numbers 0 1 2 3 4. (note - not 5, there is no 5 in the array)

finally get the result, a random item from your array

``````var result:int;
result = choices[ myRandomIndex ];
``````

you can do this with ints, Strings or anything. the array “choices” could be Strings, floats or whatever you want.

it is extremely common in games to see things like …

var choices:String = [ “Zombie”, “Zombie”, “Zombie”, “Zombie”, “Asteroid” ];

you can see that it will USUALLY be a Zombie, but occasionally an Asteroid. (You’d likely use an “Enum” here, learn about those later.)

In your example you’d simply have two items, var choices:int = [ 1, 29 ];

In normal programming languages you’d make a little extension something like choices.pickOne(),

and/or you’d make a little utility function pickOneOfThese(“Zombie”, “Zombie”, “Zombie”, “Zombie”, “Asteroid” );

this sort of thing is used constantly everywhere, in game programming. You have to be extremely familiar with every possible aspect of choosing random things, numbers, times, etc etc, and all the common idiom for this sort of thing.

OK?

If you really just want two numbers, simly use something like this:

``````if(Random.value<0.5f)
chosenNumber=42;
else
chosenNumber=4711;
``````

Random.value resturns a value between 0 and 1, so by shifting 0.5 you could also modify the probability of the two numbers.

If you want a more flexible approach, see @Fattie’s post.

Hey, it’s 2022. and If you (not the poster) were looking for something different. Check this out.

``````float RandomXDir()
{
var randy = Random.Range(1, 4);
float randomXDir = 0;

if (randy == 1)
{
randomXDir = 10;
}
else if (randy == 2)
{

randomXDir = -10;
}
else if (randy == 3)
{
randomXDir = 0;

}

return randomXDir;

}//RandomXDir
``````

then just put RandomXDir() inside whatever X. Like:

``````Vector3 victor = new Vector3(RandomXDir(), transform.position.y, RandomZDir());
``````

So later you can:

``````Instantiate(balls, victor, balls.transform.rotation);
``````

To pick between 1 and 5, I would use %5 + 1.

15%5 = 0 + 1 = 1
|16%5 = 1 + 1 = 2
|17%5 = 2 + 1 = 3
|18%5 = 3+1 =4
|19%5 = 4+1 = 5

Randomly generate a number, % the distance between lowest number and highest number (1,2,3,4,5 = 5), then add the lowest number.