What should we do if we want to create a custom random number generator class?

Can anyone explain how to create a simple class that produces random numbers, I mean a custom class without including CSDLIB and pre-defined rand() function?
My purpose is to understand rand() function behind the scenes. What should we do if want to implement a basic rand() function.
Can I find the source code for rand() function and CSTDLIB library in code::blocks folder or any other IDEs?
Thanks in advance.
A highly contrived version of a random number generator could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct lcrng {
  lcrng(int x0 = 1, long long m = 2147483647ll,
        long long x = 16807ll, long long c = 12345ll)
    : x0(x0) , m(m)
    , x(x)   , c(c) {}

  int operator()()
  { return x0 = ((x * x0) + c) % m; }
private:
  int x0;
  long long m, x, c;
};

# include <iostream>
int main() {
  lcrng my_prng;
  for (int i = 0; i < 10; ++i)
    std::cout << my_prng() % 100 << '\n';
}


You can start by reading the Wikipedia page regarding linear congruential random number generators.
https://en.wikipedia.org/wiki/Linear_congruential_generator

Knuth also writes about these for a while in The Art of Computer Programming: Seminumerical Algorithms.

See this answer re. how to get the source code:
http://www.cplusplus.com/forum/general/219746/#msg1011858
Thank you so much mbozzi for the code, but where to find the real implementation of rand() function and CSTD library or CTIME library in code::blocks installed directory? I mean the source code for them?
Last edited on
mbozzi wrote:
See this answer re. how to get the source code:
http://www.cplusplus.com/forum/general/219746/#msg1011858
Thank you, but I did't mean the header files I meant the source code, the implementations not definitions.
If you mean glibc's implementation, you can download the source here:
http://ftp.gnu.org/gnu/glibc/
The latest version is glibc-2.26.tar.gz (or .bz2 or .xz).

Download it and unpack it, look in the stdlib directory. The actual implementation is in random.c (rand.c just calls that function), random_r.c (called from random.c), and rand_r.c.

They are more complicated than that shown above. And RNG's can get more complicated still. If you need a good one it would be madness to write it yourself. Unless you are programming in C it's not recommended to use rand. C++ has much better versions.

There are also hardware sources of randomness, of course.
https://fossies.org/ hosts a online copy of most of the API documentation - if you're not keen on downloading all of it at the same time.
https://fossies.org/dox/all.html
Do a search for "glibc", or just go here, which is where the fancy stuff happens:
https://fossies.org/dox/glibc-2.26/random__r_8c_source.html#l00341
The simpler (faster, likely lower quality) libc (FreeBSD) implementation is here:
https://svnweb.freebsd.org/base/stable/11/lib/libc/stdlib/rand.c?revision=302408&view=markup#l48
if you are trying to make a better mousetrap here, you will want to run a few million values and do statistics on your randomness. There was long ago a set of tests called diehard, but that mar or may not still exist. You also have to think about if you will allow repeated values and whether you want a normal, uniform, etc distribution.

rand is probably linear congruential (sp). Which is a simple prime# & modulo generation, and not very random. (someone already said this).

Making a function that passes statistical tests is not too hard. It is however challenging to do it in a FAST function when you need a great many values. It is tempting to code up a 2 page monster that takes forever (relatively, of course) to execute.
Last edited on
> if you are trying to make a better mousetrap here,

The clearly stated goal is: "create a simple class that produces random numbers".
There is not even the slightest intent to make a better mousetrap here.

Just pick up any reasonable, simple LCG, which has been in use for some time, and then wrap a class around it.
Like mbozzi had done here: http://www.cplusplus.com/forum/beginner/219779/#msg1011945
Wow, a lot of information to digest.
Thanks for your pretty sweet answers.
If you want to have some very simple fun, make a PRNG using von Neumann's middle-squared method.
Topic archived. No new replies allowed.