This section is about using random numbers. But, really, it’s more examples using ifs and programming tricks. Random numbers are to give us more things to do. And they’re just fun to use.
The command to roll a random number is Random.Range, and then the numbers you want to roll between:
n = Random.Range(1.0f, 10.0f); rolls a random number between 1 and 10. It’s
a float, so it could be 1.35 or 6.93201.
The program below tests this in Update. It sprays out random numbers between 2 and 6:
For now, the command just works that way. The only thing we need to know is that we can change the two numbers inside.
Here are more of the rules:
This example shows how variables and assignment statements work the same as always. We roll a random number once, using Start, then print the variable over and over in Update:
All of the magic is the way Random.Range(100.0f, 200.0f) could be anything. But once it picks a number, it’s just a number. n doesn’t change by itself, since variables never do that. It’s not “infected” with randomness.
And, of course, if you ran this over and over it would print different numbers, but
always the same number repeated.
This next example is the same idea. We roll a number at random, print it, then add 1 and print it. The second number is always 1 more than the first. The trick is, don’t think “one more than a random number.” Think “one more than n2”:
This last example shows we can roll any range we want at any time, and they don’t interfere with each other or “carry over”:
a is always between 3.5 and 4.5, and b is always something in-between plus and minus a thousand.
We can make the moving, wrap-around code more interesting with random rolls. If have make a variable for the y position, we can roll it randomly after each lap:
It rolls random y once at the start of each lap. This means it stays perfectly level as it moves right. y won’t change again until the next lap.
Because of how random works, it might be near the bottom for a few laps, or appear to restart in the same place a few times. But if you let it run (increase the speed if you want) you should see it jump around.
A fun thing is we know our Cube is just snapping to a new y. But if
the edges are set right, it looks like a new Cube coming in on a different
Another thing we can do is pick a completely random spot on the screen, by randomly picking x and y. This uses a delay counter to pop anywhere at random about once a second:
Suppose we want the random position to alternate left side / right side. If we save the x position, we can check which side we’re on now with if(x<0), then roll the opposite:
I just picked 1.5 as a number far enough from 0 that we can easily tell which side we rolled. Also note how -7 is first in Random.Range(-7.0f, -1.5f), since it’s the smaller number. That’s easy to mess up with two negative numbers.
This randomly rolls the width and height of a Cube, about once a second. It’s really no different than rolling a random x/y position, but it looks different:
This might make a square, if it happened to roll width and height close together, but it will usually make rectangles.
If we wanted a random square, we could roll one size and use it for width and height:
I like this example, because it shows that even random numbers work the way we program them. We can roll once for each side, or once for both, or anything else we can think of.
This rolls a completely random color. 0-1 for red, green and blue:
A completely random color is usually an ugly grayish brown. A trick is to pick a good color, and roll close to those values.
This rolls a random “orangey” color by picking a smaller range for each slot:
Another method is to pick a random “brightness”, and multiply the original orange color by it. We always get the same shade of orange, but brighter or darker:
Colors are harder to tell apart than sizes or positions. In the first program, I can see some red and yellows pop up. The second one has more light/dark difference, but always looks pretty orange, to me.
I use tricks like this to give every brick in a wall a slightly different color. Sometimes one way just doesn’t look right, and the other way does.
Way back, we could take x,y movement code and change ySpd to make it go diagonal. But we had to do it by hand. Now we can roll it randomly. Then we also randomize xSpd and have it randomly travel only 1 to 8 across. It rerolls all three things each time it ends a lap. It seems complicated, but watching it run should clear it up:
The trick is it only rerolls the speeds at the end of a lap. While it moves in one
lap, xSpd and ySpd don’t change, so it gets a straight diagonal for each
The examples using a delay counter always used 80. We can roll that randomly. This pops the Cube to random spots, with a random delay between pops:
There’s no built-in heads/tails coin flip, but we can make one. The usual way is to roll a random 0 to 1 and check for above 0.5.:
Here’s the code to pop us around randomly, but it flips a coin for each delay. This always waits either 60 or 140 between pops:
I like this because there’s no built-in way to randomly roll 60 or 140. But, we can
make a plan: “flip a coin, heads=60, tails=140.” If you watch it run, you really can
see that it’s using 2 delay lengths.
This next example flips a coin twice, just to show we can. Each time the Cube wraps around, we restart the lap either high or low, and turn either red or green:
Again, the way randomness works, this might get high+red a few times in a row,
or get green the first four times. But if you wait enough laps it will even out and
you’ll see high/green, low/green, high/red and low/red.
I snuck in a tricky part. Notice how the first roll uses the declare-and-assign
shortcut: float coinFlip=Random …. I reuse it for the next flip with just
coinFlip=Random …. I had to remember not to double-declare coinFlip.
Moving on, a coin flip is really just a 50% chance. If we want a 10% chance we can change it to if(coinFlip<0.1f). That’s a tiny programming change, but it let’s us do different stuff.
This changes the 60/140 delay so give an 85% chance for a short delay. The only code change is 0.5 becomes 0.85. But when you watch it, it seems like the Cube is popping at an exact interval, then “whoa” – it surprises you by staying in place extra-long:
If we have more than two choices, we can make a table using a cascading if. Here’s a table I made up for how long the delay should be:
Here’s the cascading, range-slicing if to make the table. We roll a 0 to 1 percent, then see which one it is:
Notice how there’s only one roll.
Here’s the same idea, but with random treasure (the usual way games do it – mostly cheap stuff, and only a 2% chance for the top prize):
We can mix and match these ideas. In this next example, I want something more complicated for where to put y when we wrap around: 70% of the time, y should be 0, otherwise flip a coin for high or low.
This has got 3 levels of random. Even after it rolls “not the center” then gets the coin flip for “below,” it still rolls for exactly how much below. This type of random looks different than a single anywhere roll. Over time it’s obvious that it favors the center, but avoids the zone near the center.