Exploring the Production and Use of Random Numbers in C++

In summary, computers are not capable of producing truly random numbers, as they are deterministic machines. Instead, they produce pseudorandom numbers using algorithms based on congruences from an earlier internal state, seeded by the clock. 'Hardware random' information can be obtained for special purposes, such as using a Geiger counter and radioactive source to produce truly random bits. However, even these methods still rely on a starting seed value, which is not truly random. The concept of using a computer to generate the "nth" digit of pi may seem paradoxical, but it is simply a complicated but deterministic hash function. Due to the pigeonhole principle, it is impossible for a computer to explore all of pi, meaning that at some point, it
  • #1
kant
388
0
Can random numbers be produced by computers? In c++, you have functions like rand(), srand(), time(0) that more or less extract series of random numbers from a random number table. How do people produce the table in the first place?
 
Technology news on Phys.org
  • #2
Detrmanistic machines cannot produce truly random numbers; they produce pseudorandom numbers. Usually these are based on congruences from an earlier internal state, seeded by the clock.

'Hardware random' information can be obtained for special purposes.
 
  • #3
can you clarify these two points:



Usually these are based on congruences from an earlier internal state, seeded by the clock.



and



'Hardware random' information



thanks
 
  • #4
Hardware randomness is like hooking your comoutr up to a Geiger counter and a radioactive source -- you're just sending it real random bits.

The pseudorandom number generator often works like this:

new state = ((old state * big number) modulo (other number)) + large number
 
  • #5
Using CRG's example, a seed sets the "old state" to a beginning value -
Code:
old state = seconds since Jan 1 1970 + process id
new state = ((old state * big number) modulo (other number)) + large number
Hardware randomness uses arbitrary "events" in an operating system based on low-level computer hardware activity as a basis for creating a stream of bits. google for Matt Blaze's truerand program as an attempt at this sort of thing.
 
  • #6
kant said:
Can random numbers be produced by computers?
There are algorithms to generate the "nth" digit of pi, the sequence would repeat unless "n" were stored and continued to be incremented each time the generator was used.
 
  • #7
Jeff Reid said:
There are algorithms to generate the "nth" digit of pi, the sequence would repeat unless "n" were stored and continued to be incremented each time the generator was used.

This almost sounds paradoxical at first: the concept of using a discrete computer to "look into" the depths of pi and pull out truly random numbers. There are theorems which should prevent this kind of true randomness from ever coming from a determistic computer.

The resolution of the paradox is to realize that the initial seed value -- the index n into pi with which your generator begins -- is not truly random, but only psuedorandom.

Viewed in this light, pi is nothing more than a giant table of truly random numbers, computed on-demand, and the hard part of the problem is picking a truly random n to start reading it. Since no computer will ever be able to choose a truly random starting n, the resulting algorithm's output is still not truly random.

- Warren
 
  • #8
chroot said:
Viewed in this light, pi is nothing more than a giant table of truly random numbers, computed on-demand, and the hard part of the problem is picking a truly random n to start reading it. Since no computer will ever be able to choose a truly random starting n, the resulting algorithm's output is still not truly random.

Alternately, the process of determining the nth digit of pi is just a complicated but determanistic hash function. :biggrin:
 
  • #9
There's also a pigeon-hole problem here: a computer with 2^10 bits of memory, for example, can only choose one of 2^10 different starting points for n. It would be impossible for a computer of finite memory capacity to truly explore ALL of pi. This means that, at some perhaps distant time in the future, the computer will select a value for n that was previously selected.

You can do all sorts of mixing and hashing and other procedures to eliminate periodicity in the seed, but, eventually, you will always end up producing nearly random numbers. Of course, nearly random numbers are not random, though you could design a system to produce numbers to any desired degree of "randomness."

- Warren
 
  • #10
chroot said:
There's also a pigeon-hole problem here: a computer with 2^10 bits of memory, for example, can only choose one of 2^10 different starting points for n. It would be impossible for a computer of finite memory capacity to truly explore ALL of pi. This means that, at some perhaps distant time in the future, the computer will select a value for n that was previously selected.

I agree with you that the pigeonhole principle alone means that determanistic machines can't produce randomness. Your equation is off, though. A computer with 210 = 1024 bits of memory can choose not
[tex]2^{10}=1.024e3[/tex]
starting places but
[tex]2^{2^{10}}\approx1.798e308[/tex]
starting places.
 
  • #11
CRGreathouse said:
[tex]2^{10}=1.024e3[/tex]
starting places but
[tex]2^{2^{10}}\approx1.798e308[/tex]
starting places.

You lost me there. What are you talking about?

- Warren
 
  • #12
chroot said:
You lost me there. What are you talking about?

Consider base b numbers. How many n-digit numbers are possible?

b^n, e.g, in base 10, 10^3 3-digit numbers are possible.

Hence, 2^(2^10) 2^10-digit binary numbers are possible.
 
  • #13
George Jones said:
Consider base b numbers. How many n-digit numbers are possible?

b^n, e.g, in base 10, 10^3 3-digit numbers are possible.

Hence, 2^(2^10) 2^10-digit binary numbers are possible.

Exactly, thanks for the explanation.
 
  • #14
Heh, of course.

- Warren
 

FAQ: Exploring the Production and Use of Random Numbers in C++

How are random numbers generated in C++?

In C++, random numbers can be generated using the rand() function from the cstdlib library. This function returns a random integer between 0 and RAND_MAX, which is a constant defined in the cstdlib library.

Can the range of random numbers be changed in C++?

Yes, the range of random numbers can be changed in C++ by using the srand() function to set a new seed value. This function takes in a seed value as a parameter, which can be used to generate a different sequence of random numbers.

How can I generate random floating-point numbers in C++?

C++ also has a rand() function that can be used to generate random floating-point numbers. However, this function only returns integers, so to generate floating-point numbers, you can use the static_cast function to convert the integer to a float value.

Can I generate a specific range of random numbers in C++?

Yes, you can generate a specific range of random numbers in C++ by using the modulo operator. For example, if you want to generate random numbers between 1 and 10, you can use rand() % 10 + 1.

Are the random numbers generated in C++ truly random?

No, the random numbers generated in C++ are not truly random. They are generated using a deterministic algorithm and are based on a seed value. However, they can appear to be random enough for most practical applications. For more reliable and truly random numbers, specialized libraries or hardware generators can be used.

Back
Top