3

Note: This post is not a duplicate of how to generate random numbers in C, it does not answer my question.

I have a function which is:

double generator(double * ran_i) {
  srand(time(NULL));
  return ran_i[rand()%1000];
}

which takes an array containing 1000 numbers, then I try to call:

for(i=0; i< 1000; ++i) {
  printf("%lf", generator(ran[0]));
}

However in that loop it always gives me the same number. I figured out that time(NULL) does not change the seed fast enough. How can I change this seed faster or even better change it in every loop? Also I can only use libraries compatible with ANSI C standard.

Mali. G
  • 41
  • 1
  • 5
  • 3
    instead of using different seeds you should seed the rng only once. It is the job of the rng to provide you a sequence of random numbers, the seed is only to determine the starting point in that sequence – 463035818_is_not_an_ai Apr 30 '17 at 09:46
  • 3
    You don't change your seed, period. Why do you think you want to do that? – n. m. could be an AI Apr 30 '17 at 09:50
  • The suggested duplicate contains the answer to your problem, though not your question. Seed the PRNG once. reseeding with the same value gives you the same sequence. If you insist on reseeding, what for? – Hasturkun Apr 30 '17 at 09:51
  • 2
    [srand() — why call it only once?](http://stackoverflow.com/q/7343833/995714) – phuclv Apr 30 '17 at 09:58
  • I'm not sure what your quibble with the marked duplicate is; it's asking about almost exactly the code you ask about. And the most appropriate answer is the same to both. –  Apr 30 '17 at 09:59

1 Answers1

0

I figured out that time(NULL) does not change the seed fast enough.

That would be accurate. time returns the current timestamp in seconds, so it changes once every second.

How can I change this seed faster ...

You would need a source of randomness. On POSIX you might use /dev/urandom, but that is of course not necessarily available to all platforms that support ANSI C.

So, the conclusion is that there is no API in ANSI C to access a purely random seed source, so getting a new, random seed faster than once a second is not feasible in ANSI C.

... even better change it in every loop?

Seeding a pseudo random number generator on every call is silly - don't try to do it. Just seed once, at the start of the program. (You can re-seed if you need to repeat a sequence).

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 3
    I feel like "Besides, seeding on every call is silly - don't try to do it." should be more prominent in every answer. Whatever you do might solve the question asked, when in fact the question should not even be posed as it is going into a completely wrong direction. – mrks Apr 30 '17 at 09:52
  • what you wrote isnt wrong, but could be misunderstood. seeding a prng on every call isnt just silly, but it is definitely not the way it is supposed to be used. You could mention what is the correct solution to the problem – 463035818_is_not_an_ai Apr 30 '17 at 09:55
  • 2
    @tobi303 The correct solution is the last sentence: Don't try to seed a pseudo random number generator on every call. But point taken, I'll add that they should seed once :) – eerorika Apr 30 '17 at 09:56
  • I know, but I am not sure if this will help OP to fix his code. Sorry, just nitpicking here, but imho you should mention explicitly that the prng has to be seeded only once outside of the loop. That it doesnt work if seeded inside the loop OP already knows ;) – 463035818_is_not_an_ai Apr 30 '17 at 10:00
  • 1
    Bold isn't enough for emphasis; the note on seeding really should be the very first thing this answer says. Everything else is only for helping people committed to doing things wrongly. –  Apr 30 '17 at 10:02
  • 1
    @Hurkyl no, it should be the last sentence so that's what the reader remembers. The question - even if misguided in their case - still has merit. If you have a program that may run multiple times within a second and must have random behaviour, then having access to a random seed that changes faster than once a second is crucial. Unfortunately there isn't an ANSI C solution for that problem (that I know of). My answer serves not only OP, but also the people who look for the same question for the correct reason. – eerorika Apr 30 '17 at 10:05