-1

I am using ran2() function in c programming codes. ran2() is known as more truly random number generator. I found that ran2() is also generates numbers in same sequence. Why is it so?

ran2 takes a long type pointer variable.

The code of ran2() is available in Numerical recipes.

Here I'm writing how I called the function:

float temp;
long *seed,seed1;
seed=&seed1;
temp=ran2(seed);

After calling this function multiple times and running multiple times I see that numbers of same sequence are generated every time. Correct me where I am doing the mistake and what modifications needed to get the truly random sequence every time.

pjs
  • 18,696
  • 4
  • 27
  • 56
no one
  • 1
  • 1
  • 1
    What is `ran2`? Please [edit] and clarify. OT: that being said, just write `temp=ran2(&seed1);` and drop `*seed,` and the whole line `seed=&seed1;`. – Jabberwocky May 19 '23 at 15:11
  • The code of ran2() is there in "Numerical recipes in c " It is the standard random number generator scientific community use . Using ran2(&seed1) gives error as "segmentation fault (core dumped)".the function ran2 takes long pointer variable as parameter. – no one May 19 '23 at 15:29
  • Your comment rises another question: what is "Numerical recipes in C"? Please [edit] – Jabberwocky May 19 '23 at 15:32
  • 1
    Most likely, you are supposed to provide a seed value to `ran2()` — you get to choose which value to start with. If you use `long seed = 1;` and then call `float temp = ran2(&seed);` repeatedly, you will get one sequence of numbers. Change `1` to `2` and you'll get a different sequence, and so on. – Jonathan Leffler May 19 '23 at 15:33
  • 1
    Please [edit] and show a [mcve] along with the output you get. – Jabberwocky May 19 '23 at 15:35
  • 2
    The second edition of [Numerical Recipes in C](https://www.amazon.com/Numerical-Recipes-Scientific-Computing-Second/dp/0521431085/) is an old (1992) but fairly reliable textbook — but the licensing terms on the code in the book are draconian, IIRC. I decided never to use it because of that. (There is also a rather expensive [3rd Edition](https://www.amazon.com/Numerical-Recipes-3rd-Scientific-Computing-dp-0521880688/dp/0521880688/).) – Jonathan Leffler May 19 '23 at 15:35
  • Thanks Jonathan .Can you tell me how to get different sequence everytime ? There is a technique by using time function available in time.h library.Can u tell me how to use this in my case . – no one May 19 '23 at 15:42
  • It's a poor quality way of doing it, but `long seed = time(0);` will use the time as a seed value. For a discussion of other ways to generate random seeds, see [Random password generator algorithm?](https://stackoverflow.com/q/75946155/15168) – Jonathan Leffler May 19 '23 at 15:46
  • @JonathanLeffler: `ran2` requires `seed` to be negative to initialize. Otherwise it treats it as continuing state. `time(0)` is unlikely to provide that. – Eric Postpischil May 19 '23 at 15:49
  • @EricPostpischil: As I said, I've not looked at the book — the OP has the book and can find that sort of detail out for themselves by careful reading. – Jonathan Leffler May 19 '23 at 15:56
  • 1
    *`ran2()` is known as more truly random number generator* More "truly random" than what? [From 1994](https://pubs.aip.org/aip/cip/article/8/1/117/509983/Some-portable-very-long-period-random-number): "It is found that a proposed random number generator ran2, recently presented in the Numerical Recipes column [W. H. Press and S. A. Teukolsky, Comput. Phys. 6, 521–524 (1992)], is a good one, but a number of generators are presented that are at least as good and are simpler, much faster, and with periods ‘‘billions and billions’’ of times longer" – Andrew Henle May 19 '23 at 17:05

2 Answers2

2

The source code for ran2 in Numerical Recipes says “Call with idum a negative integer to initialize”. The parameter of ran2 is long *idum. So, to use ran2, define a long, set it to a negative value, and pass a pointer to it to ran2. In non-sensitive uses, it is not uncommon to use the standard C time function to get a value to use to initialize random number generation. You will need to ensure it is negative. So you can use:

long seed = time(0);
if (0 <= seed)
    seed = -1 - seed;
…
float x;
…
x = ran2(&seed);
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

The state of the art of pseudorandom number generation has advanced considerably since Numerical Recipes was published 30 years ago. I don't know how ran2 works but I can practically guarantee there are better choices nowadays.

Unless you are trying to reproduce results from an old piece of research that specifically used ran2, I strongly recommend you do not use it. Instead, your first choice should be the keystream from a current-generation stream cipher, e.g. ChaCha20. In the very unlikely event that that isn't fast enough for your application, MELG-64 is probably what I would try second.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    Even immediately on publication, there were responses [like this](https://www.uwyo.edu/buerkle/misc/wnotnr.html): "The NR-recommended random number generators RAN1 and RAN2 should not be used for any serious application. If you use the top bit of RAN1 to create a discrete random walk (plus or minus 1 with equal probability) of length 10,000, the variance will be around 1500, far below the desired value of 10,000." and – Andrew Henle May 19 '23 at 17:10
  • (cont) "Both are low-modulus generators with a shuffling buffer, in one case with the bottom bits twiddled with another low-modulus generator. The moduli are just too low for serious work, and the resulting generators even out too well" – Andrew Henle May 19 '23 at 17:10
  • Thanks for the information .I will surely look on it . – no one May 19 '23 at 19:00