Generating Random Numbers?
31 October 2022<br>Generating Random Numbers?
Well before we dive into procedural generation we need to know what exactly is procedural generation, right? Well procedural generation is letting the computer generate something based on certain parameters we give it as an input.
Now where do we need randomness? Or do we really need randomness for procedural generation?<br>The short answer is no we don’t.
However if as we get into more complicated things randomness plays a very important role. Randomness makes out procedurally generated creations interesting. As I said before procedural generation is basically giving the computer certain parameters and letting it generate something for us. So a simple use of randomness will be randomly generating those parameters using certain rules and we have a system that can generate several different but similar in nature results easily. Now this is just a simple example of using random numbers, as we learn more about procedural generation we see more uses so for now lets see what these random numbers are and how can we get them?
Now (almost)every programming languages standard library provides some sort of function to generate random numbers.<br>For example:
Math.random(); // Java & JavaScript<br>rand(); // C/C++<br>System.Random // C#<br>random.randrange(0, 10) // python<br>Now if you are reading this article about procedural generation I am pretty sure you have used some of these random number functions before.
But have you every thought about how they work?
I mean lets think how can we say something is a random number?
Ask yourself to tell a random number?
How did you do it?
Computers are not intelligent beings like us. They just run algorithms. So computers cannot really generate a random numbers!
But we do have these functions which gives us random seeming numbers, right?<br>Well these functions internally use some algorithms internally to generate these random seeming numbers.<br>What I am calling random seeming numbers are properly termed pseudo-random numbers .
Now since we are using algorithms to generate these, so are they predictable, right?<br>The answer would be kind of .
NOTE: Every different language’s standard library has their own method to do this so what I am talking about is the general idea.
To understand what I mean we need to take a look at some of the methods to generate the random numbers.
Before going into some proper or sophisticated method lets try to deduce some of our own methods to random numbers.
Using Irrational Numbers
As you might guess irrational numbers like PI or the Euler’s number are a decent source of randomness.
Lets see PI :
3.14159265358979323846264338327950288419716939937510<br>Here you can see that the digits of PI are seemingly random. Now whether they are truly random or now is a topic for further debate, but for simple things this randomness is enough.
A simple random() function
Now lets try to implement a simple function that gives a random number in range [0.0, 1.0]<br>Lets think mathematically are there any functions that has some randomness.
What’s the simplest one that comes to your mind?
The simplest such function that comes to my mind is:
f(x)=sin(1x)f(x) = \sin ( \frac{1}{x} )f(x)=sin(x1)<br>It looks like:
So here you can see it quite random in the range [-0.05, 0.05].
Now how to utilize this?
Well for every random number function there is something called a seed.
What is a seed?<br>Well its any number provided to the random number generator with respect to which our function generates a random number. What can be the seed? anything 1, 2, 3, … and no matter how the seeds are related be they in AP (Arithmetic Progression) or anything the number returned by a good random number generator will be totally random and will not be increasing or decreasing with the seed!
Now what about the seed in the random number functions we use in various programming languages?<br>Well they are there but some are initialized by default by the library itself and some are not. And why is the seed not required for every call to the random function in out libraries?
Well there are 2 simple possible answers (there may be more but for simplicity sake lets only deal with these 2):
A new seed is somehow generated? How ? (take a guess : the current time is a decent seed right?)
The last randomly generated number is the seed for the next!
Extra Info: For C or C++ rand() if you just use it you will face confusing issue that every time the program is run the set random numbers is the same! Here is the solution to that.
srand((unsigned int)time(NULL)); // add this somewhere before rand() is called<br>What this does is set the seed to the random number generator to the current time.
Now back to our random function, we need to somehow map our seed the [-0.05, 0.05] range.
What is the easiest way to do it? -> sin or cos
So our new function becomes:
f(x)=sin(10.05cosx)f(x) = \sin ( \frac{1}{ 0.05 \cos x }...