# Thread: Create my own rand function?

1. ## Create my own rand function?

There is one question in my mind. What's the logic behind rand function. That is how can we write our own rand function.

One approach may be to use garbage value of variables
Code:
```int main()
{
int a[10];
for(int i=0;i<=10;++i)
cout<<a[i];
}```
But problem with this code is that it will always print same numbers. Is garbage value a fixed value at a memory location. That is say at memory location X if not initialised i will get same value each time. What can be reason behind this?

2. > One approach may be to use garbage value of variables
1. even reading undefined values may cause exceptions
2. you run off the end of the array

Common implementations of rand() use this
http://www.physics.orst.edu/~rubin/n...mc3/node2.html
Such algorithms are only any good for student exercises.

For statistical quality rand functions, consider this one
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

For cryptographic quality rand functions, you really need an entropy pool based approach, like
http://publib.boulder.ibm.com/infoce...les/random.htm

3. I suggest that you read what Prelude has to say about Random Numbers.

4. It is very surpising and shoking to see that there is same garbage value at a position in main memory. No matter how many times I run above program I get same result?
Any idea why it is so

5. It is very surpising and shoking to see that there is same garbage value at a position in main memory. No matter how many times I run above program I get same result?
Any idea why it is so
Because your program happens to read from the same memory location each time it is run? I do not think that this is the right approach especially since the contents of memory may not have very random properties to begin with.

6. Originally Posted by Mario F's Signature
Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin [John Von Neumann]
Yup.

7. Originally Posted by Jesus
Let anyone among you who is without sin be the first to throw a stone at her.
Some people call them 'pseudo-random numbers', but they are still in a state of sin either way

8. Code:
`Because your program happens to read from the same memory location each time it is run?`
But even after restarting my computer,I get same result. Is it such that garbage value is constant for a particular memory location.

9. Do yourself a big favor and don't try to read variables that don't store anything yet. (Okay an array could be called one variable, but don't pick on me; it's a collection of variables the same size given a single name.) It's dangerous.

10. But even after restarting my computer,I get same result. Is it such that garbage value is constant for a particular memory location.
Your BIOS runs a RAM test every time you start your computer. The data may be left-over from the RAM test, or it may be left-over from something that Windows was doing during start-up.

I'll bet if you did a statistical analysis of all the numbers in memory, some numbers would show-up more frequently than others. Small numbers would be more common than large numbers. Numbers that represent ASCII characters would be common. Numbers that represent CPU op-codes would be common, etc.

11. Hmm...,that seems to be quite logical.But can I check it

12. Also, in debug mode, the compiler tends to insert code to fill memory with known values, just so that it can check a few things.

13. Originally Posted by Salem
> One approach may be to use garbage value of variables
1. even reading undefined values may cause exceptions
2. you run off the end of the array

Common implementations of rand() use this
http://www.physics.orst.edu/~rubin/n...mc3/node2.html
Such algorithms are only any good for student exercises.

For statistical quality rand functions, consider this one
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

For cryptographic quality rand functions, you really need an entropy pool based approach, like
http://publib.boulder.ibm.com/infoce...les/random.htm
Good suggestions, Salem!

I would like to add (if the OP is open to more suggestions) that NETLIB has many random number generation routines. They happen to be written in FORTRAN, but they are pretty easy to translate into C++.

Also, Boost has some random number libraries too:

Regards,

David

14. a couple of years ago (when it released the p3) intel was talking about implementing a rand assembly instruction that took a truly random physical value (chip heat, electrical interference,etc) and returned it. Anyone ever hear what happened to that?

BTW if you want a truly random value, get a geiger counter and some radioactive material and hook it up to your pc.

15. www.random.org

But please be polite, and don't use up all of them

rand() uses a math algorithm, which doesn't provide true random numbers, but a sequence of numbers which at first glance APPEAR to be random, and pass statistical tests that random numbers pass.

You could read up on the Mersenne Twister algorithm and try implementing it yourself. Good luck.