# Is there any function better than rand()

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-30-2005
ubern00ber
Is there any function better than rand()
Is there any function better than rand() that can still get random numbers? If there isn't one, can i make my own in any way?
• 06-30-2005
achacha
There are lots and lots :)

Try: http://www.agner.org/random/ as a start.
• 07-01-2005
Dae
Theres this one, for C though (barely any C only code though).

http://thorkildsen.no/faqsys/docs/random.c //Labelled the "best" method, but it was written a while ago so who knows.
• 07-01-2005
Antigloss
Quote:

Originally Posted by Dae
Theres this one, for C though (barely any C only code though).

http://thorkildsen.no/faqsys/docs/random.c //Labelled the "best" method, but it was written a while ago so who knows.

I dont know if it's the best method. But I do know the author is a void mainer.
• 07-01-2005
mitakeet
First you have to define what you mean by 'good' so you can then evaluate 'better'. What are you going to use the random numbers for? The criteria for cryptography is much different than that for long-running simulations.
• 07-01-2005
DougDbug
In 99% of situations, rand() is good-enough if you seed it with time. All you need is a number that's unpredictiable. rand() is unpredictible enough for most games and simulations. You would generally need a large number of samples before you start to see a pattern or a trend. ...If you're gambling, you'll run out of money long before you figure-out how to out-smart rand()! :D

Before you try to improve-on rand() you should determine what it is about rand() that needs improving. And, your version should not have weaknesses or limitations that are not present in rand().

Just for fun... from the C language standard: *
Quote:

7.20.2 Pseudo-random sequence generation functions

7.20.2.1 The rand function

Synopsis
1
Code:

```#include <stdlib.h> int rand(void);```
Description
2 The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.

3 The implementation shall behave as if no library function calls the rand function.

Returns
4 The rand function returns a pseudo-random integer.

Environmental limits
5 The value of the RAND_MAX macro shall be at least 32767.

7.20.2.2 The srand function

Synopsis
1
Code:

```#include <stdlib.h> void srand(unsigned int seed);```
Description
2 The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

3 The implementation shall behave as if no library function calls the srand function.

Returns
4 The srand function returns no value.

5 EXAMPLE The following functions define a portable implementation of rand and srand.
Code:

```static unsigned long int next = 1; int rand(void) // RAND_MAX assumed to be 32767 {     next = next * 1103515245 + 12345;     return (unsigned int)(next/65536) % 32768; } void srand(unsigned int seed) {     next = seed; }```

*The C++ standard simply refers-back to the C standard.
• 03-08-2006
SlyMaelstrom
Not to be the one to toot another member's horn, but this is a good read:

http://eternallyconfuzzled.com/articles/rand.html

and here is their rand library:

http://eternallyconfuzzled.com/libs/jsw_rand.html
• 03-08-2006
OnionKnight
MTwister is fast and has a period of 2^19937-1
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html
• 03-08-2006
7stud
Quote:

Is there any function better than rand() that can still get random numbers?
I work in the encryption department for NORAD as a NATO liason, and I use this random number generator:
Code:

```cout<<"Enter a number: "; int randNum=0; cin>>randNum; short presidentsCode = encryptTheLaunchCodes(randNum);```
gdubya likes to keep it simple.
• 03-08-2006
Sebastiani
smartass
• 03-08-2006
laserlight
7stud does have a point though. One could go and do something like the HotBits setup.
• 03-09-2006
major_small
I've seen people discuss using static (as in using a radio tuned to nothing) to generate randomness...
• 03-09-2006
what3v3r
just an idea... :D

can i use an unallocated memory, or a random memory address, cast what3v3r values it has as an integer and use it as a random number. provided that it will not change it in anyway just read and maybe copy to a variable?

crazy idea... :D (please don't flame me...)

TIA.
• 03-09-2006
Salem
> can i use an unallocated memory, or a random memory address
No.
Even just reading random addresses which you don't own can cause things like segmentation faults.
• 03-09-2006
mitakeet
Quote:

Originally Posted by what3v3r
just an idea... :D

can i use an unallocated memory, or a random memory address, cast what3v3r values it has as an integer and use it as a random number. provided that it will not change it in anyway just read and maybe copy to a variable?

crazy idea... :D (please don't flame me...)

TIA.

Memory contents are very poor sources of randomness. In particular, most OSs will zero memory before providing it to a process, which makes the memory non-random in the first place. Second, it is a very rare data set that randomly distributed data and binaries are far from random. However, if you want to run a hash algorithm (like SHA-256) on a chunk of memory then you might be able to use that as the initiation to a pseudo random number generator. You would have to build the functionality into the Kernel as only the Kernel has full access to memory.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last