truly random number

I assume the reason why the number(s) are static is because I'm doing something wrong...

1
2
int x = rand();
cout << x;


Output:
41


How do I make a truly random number?
Seed the generator once at the start of your program. Although even then it will only be pseudo-random. You'd have to go to something like radiation if you want "truly random" numbers.
radiation? LoL.... Wait, you serious? o_O?

I'm thinking there can be something like, check the memory of a random process and rand() % int memory size, would that be possible?
Copied straight from the website http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* srand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
  printf ("First number: %d\n", rand() % 100);
  srand ( time(NULL) );
  printf ("Random number: %d\n", rand() % 100);
  srand ( 1 );
  printf ("Again the first number: %d\n", rand() %100);

  return 0;
}


If you have an application that needs a random number once during its life time then you could get a random number by grabbing the millisecond of the current time.
Sure, but it wouldn't be "truly" random (nothing is actually random, they just appear to be because of the complexity of the process, or number of variables involved).

EDIT: @James:
You should only call srand once in your program.
Last edited on
Thanks, as mentioned it is straight from the website :)
Shay999 wrote:
check the memory of a random process

Erm, you'd need something to generate a random memory address/process to do that.
I'm thinking there can be something like, check the memory of a random process and rand() % int memory size, would that be possible?
Sure, but exactly how many processes do you expect to run on the system. What if you need to generate millions of numbers?

What firedraco was stalking about, by the way, was that, among a few other things, the intervals between clicks of a Geiger counter are considered truly random. Signals from the CMBR are also considered random.
Pure software random number generators are actually pseudo-random number generators (PRNG), because their behavior is only seemingly random.
Zhuge wrote:

Erm, you'd need something to generate a random memory address/process to do that.


It appears we have a circular dependency... We need a RNG to get a value to seed our RNG.
Topic archived. No new replies allowed.