Random Bool

How do you set a bool randomly to true or false? I have:

int maxX = 15;
		int maxY = 15;
		float tileSize = 3;
		for(int ynt = 0; ynt < maxY; ynt++)
			for(int xnt = 0; xnt < maxX; xnt++)
			{
				{
					Vert theVert = new Vert();
					Verts.Add(theVert);
					theVert.Position = new Vector2(tileSize*xnt, tileSize*ynt);
					Instantiate(Grass, new Vector3(tileSize*xnt, tileSize*ynt, tileSize),Quaternion.identity);

At the bottom I know I need somthing along the lines of:

theVert.vertActive = /* random(1,0);*/

But I have tried so many things and they just don’t like to work. Any ideas?

Try this

theVert.vertActive = (bool)Random.Range(0,1);

You would actually need Random.Range (0, 2) because the upper limit is never returned.

Unity does seem to like random or .range

if says
Assets/PathArray.cs(96,68): error CS0104: Random' is an ambiguous reference between UnityEngine.Random’ and `System.Random’

Assets/PathArray.cs(96,68): error CS0103: The name `Random’ does not exist in the current context

Ah true, Also you cant cast from int to bool. So do this:

System.Convert.ToBoolean( UnityEngine.Random.Range( 0, 2 ) )
1 Like

This is also a simple way to do it

bool randomBool = Random.value > 0.5f;
2 Likes

Ahhh Nice, perfect thanks a lot!

Take out “using System;” If you really want to have that, then you’ll need to specify UnityEngine.Random.

–Eric

1 Like

I’ve found Unity’s Random.Range to not be so random when using small ranges of numbers.
It seems to work in ‘groups’…
So if you’re using…

Random.Range(0, 2);

It will return ‘0’, 3 or 4 times in a row, or if the range is 0 - 3, then it may return ‘2’ several times in a row.
I’ve even had it, when testing my monster truck game, often return the same 3 race variables 2 or 3 races in a row, i.e night racing, in the snow, in a clockwise direction!!

A more ‘random’ approach in my opinion to get a bool is to have a large range of numbers, then test if the number is odd or even…

i.e

bool GetRandomBool()
    {
        int randomNumber = Random.Range(0, 100);
        return (randomNumber % 2 == 0) ? true : false;
    }

Well, that’s expected (and desired) behavior. It’s random after all. There’s no statistical difference whatsoever between “Random.Range(0, 2)” and “Random.Range(0, 100) % 2”. The second method is definitely no more “random” than the first. However Antony Blackett’s method of using Random.value is the simplest and most straightforward, so just use that.

–Eric

2 Likes

I understand what you’re saying, but from the 18 months I’ve spent on this current project, Random.Range really has continuously repeated items many more times than it should have, and it certainly didn’t seem random. Even selecting a random song out of 10 songs using Random.Range, it happens very often that the same song gets played 3 times in a row! Then I go and have 4 or 5 of the same races in a row that have a 33% chance each time of being selected!

As soon as I expanded the range of values assigned to Random.Range, i.e making the random range 1-30, then assigning each of those into crisp sets, I found the music selection, and track/environment selection was a lot more ‘random’, and I hardly experience repeats any more.

/

…for what’s its worth, this is another option:
bool randomBool = Random.Range(0, 2) == 0;

Please don’t necropost.

2 Likes