Login Register






[Paper] Pseudo random number generation filter_list
Author
Message
[Paper] Pseudo random number generation #1
Pseudo-random number generation

A PRNG (Pseudo Random Number Generator) is a number generating function that returns a pseudo-random number.

What is a pseudo random number?

It's really abstract and complex to generate actually random numbers, so, these generators usually take two parameters: floor and ceiling, and they generate a pseudo-random number that's in the floor-ceiling range (it's not actually random as it cannot fall out of that range).

How does a pseudo random number generator work?

A pseudo random generation function usually follows an algorithm that takes 3 constants: seed, floor and ceiling. We've already seen what floor and ceiling are, so, let's talk about seed. The random seed is a number given to initialize the algorithm. The scope of the algorithm is usually somehow to manipulate or elaborate a k number to obtain a pseudo-random number between floor and ceil. k is often renewed by the algorithm itself at each iteration, so that, for each call to the function, even using the same floor and ceil, the manipulation will lead to a different pseudo-random number, else, a (pseudo)code like this:

Code:
print(rand(1,10)) print(rand(1,10)) print(rand(1,10))

would output the same number for three times, and pseudo-randomness wouldn't be so random. Suppose the pseudo-random generation algorithm is:

Code:
n = k + 1 while (n > ceil) n = n - 1 return n

Now, an actual PRNG wouldn't work like that, but, just to explain the concept, let's say k is 3, the aforementioned piece of code would output:

Code:
4 4 4

If the algorithm worked instead like this:

Code:
k = k + 1 while (k > ceil) k = k - 1 return k

The same code would output:

Code:
4 5 6

Now, to start off the algorithm, we need to define k. k is the random seed. Most generating functions will work even if you don't seed the generator in the beginning, but that won't make your pseudo-random numbers so random. A good seed is the OS time (most languages have a function that returns the OS time in seconds) as it's changing pretty often and it's independant from your program (thus, good randomness chances).

I don't know how to develop a pseudo-random generator algorithm.

You can pick up common PRNG algorithms and write your own code for them.

See http://en.wikipedia.org/wiki/PRNG .

If you want to make your own PRNG algorithm, here are some tips for you:

- use first, middle and last digits of the seed for your elaboration. If you only use the first or middle digits (like some algorithms do) seeds like the current OS time won't make your function so random (as changes are in the last digits most of the time);
- avoid recursion (like, using your own random generator function to generate random numbers to be used in the generation process), that often causes stack overflow issues in algorithms like these;
- try to avoid errors occuring in cases such as where the seed, floor or ceiling is a number that could cause trouble in your algorithm (e.g: you somewhere divide something by floor and floor is 0);
- efficiency is not the last thing to think about, don't go for a very slow algorithm;
- be sure randomness is remarkable and that there aren't just small differences in random generated numbers (e.g: 9.89, 9.67, 9.94);
- avoid using other random number generators anywhere in your algorithm (be indipendent).

What's the best language to code a PRNG?

Probably the best math support you have, the best PRNG you'll get (example: if your language of choice uses the e number for big numbers notation it might give some trouble in some cases; a language that instead uses the full "regular" number notation will be less likely to run into a bug/error).

Reply

[Paper] Pseudo random number generation #2
Pseudo-random number generation

A PRNG (Pseudo Random Number Generator) is a number generating function that returns a pseudo-random number.

What is a pseudo random number?

It's really abstract and complex to generate actually random numbers, so, these generators usually take two parameters: floor and ceiling, and they generate a pseudo-random number that's in the floor-ceiling range (it's not actually random as it cannot fall out of that range).

How does a pseudo random number generator work?

A pseudo random generation function usually follows an algorithm that takes 3 constants: seed, floor and ceiling. We've already seen what floor and ceiling are, so, let's talk about seed. The random seed is a number given to initialize the algorithm. The scope of the algorithm is usually somehow to manipulate or elaborate a k number to obtain a pseudo-random number between floor and ceil. k is often renewed by the algorithm itself at each iteration, so that, for each call to the function, even using the same floor and ceil, the manipulation will lead to a different pseudo-random number, else, a (pseudo)code like this:

Code:
print(rand(1,10)) print(rand(1,10)) print(rand(1,10))

would output the same number for three times, and pseudo-randomness wouldn't be so random. Suppose the pseudo-random generation algorithm is:

Code:
n = k + 1 while (n > ceil) n = n - 1 return n

Now, an actual PRNG wouldn't work like that, but, just to explain the concept, let's say k is 3, the aforementioned piece of code would output:

Code:
4 4 4

If the algorithm worked instead like this:

Code:
k = k + 1 while (k > ceil) k = k - 1 return k

The same code would output:

Code:
4 5 6

Now, to start off the algorithm, we need to define k. k is the random seed. Most generating functions will work even if you don't seed the generator in the beginning, but that won't make your pseudo-random numbers so random. A good seed is the OS time (most languages have a function that returns the OS time in seconds) as it's changing pretty often and it's independant from your program (thus, good randomness chances).

I don't know how to develop a pseudo-random generator algorithm.

You can pick up common PRNG algorithms and write your own code for them.

See http://en.wikipedia.org/wiki/PRNG .

If you want to make your own PRNG algorithm, here are some tips for you:

- use first, middle and last digits of the seed for your elaboration. If you only use the first or middle digits (like some algorithms do) seeds like the current OS time won't make your function so random (as changes are in the last digits most of the time);
- avoid recursion (like, using your own random generator function to generate random numbers to be used in the generation process), that often causes stack overflow issues in algorithms like these;
- try to avoid errors occuring in cases such as where the seed, floor or ceiling is a number that could cause trouble in your algorithm (e.g: you somewhere divide something by floor and floor is 0);
- efficiency is not the last thing to think about, don't go for a very slow algorithm;
- be sure randomness is remarkable and that there aren't just small differences in random generated numbers (e.g: 9.89, 9.67, 9.94);
- avoid using other random number generators anywhere in your algorithm (be indipendent).

What's the best language to code a PRNG?

Probably the best math support you have, the best PRNG you'll get (example: if your language of choice uses the e number for big numbers notation it might give some trouble in some cases; a language that instead uses the full "regular" number notation will be less likely to run into a bug/error).

Reply