Is it possible to increase the refresh speed of srand(time(NULL)) in C?
Asked Answered
B

4

6

I am asking to see if there is a way of increasing the speed the srand(time(NULL)); function "refreshes"? I understand srand() produces a new seed according to time (so once a second), but I am looking for an alternative to srand() that can refresh more often than 1 second intervals.

When I run my program it produces the result it is supposed to, but the seed stays the same for essentially a second, so if the program is run multiple times a second the result stays the same.

Sorry for such a simple question, but I couldn't find an answer specifically for C anywhere online.

Binnie answered 11/6, 2015 at 7:7 Comment(4)
Use some value derived from gettimeofday() instead of time() if you need more than second accuracy. This is completely orthogonal to srand.Unformed
The appropriate choice of function will be operating system dependentVolleyball
In addition: if you are worried about the quality of your random numbers, do not use rand.Winebaum
Get random data from random.org (use certificate authentication to prevent MotM attack)Stonebroke
P
4

You could try fetching a seed value from some other source. On a unix system, for example, you could fetch a random four-byte value from /dev/random:

void randomize() {
  uint32_t seed=0;
  FILE *devrnd = fopen("/dev/random","r");
  fread(&seed, 4, 1, devrnd);
  fclose(devrnd);
  srand(seed);
}
Porkpie answered 11/6, 2015 at 7:20 Comment(2)
Also, you don't want to use add, you want to use bitwise OR.Curtsey
It might be worth noting this is reliant on some kind of UNIX like OS.Ivanna
I
3

srand(time(NULL)); is not a function, it is rather two functions: time() which returns the current time in seconds since the epoch; and srand() which initialises the seed of the random number generator. You are initialising the seed of the rendom number generator to the current time in seconds, which is quite a reasonable thing to do.

However you have several misconceptions, you only actually need to run srand once, or at most once every few minutes, after that rand() will continue to generate more random numbers on its own, srand() is just to set an initial seed for rand to start with.

Second, if you really do want to do this, while I don't see why you would you could use a function that returns the time to a higher precision. I would suggest gettimeofday() for this purpose.

Ivanna answered 11/6, 2015 at 7:14 Comment(9)
so if the program is run multiple times a second---contradicts your second paragraph. I believe OP is not calling srand() multiple times in code itself, the program is being run multiple times.Mita
@SouravGhosh I am saying doing that is the wrong approach, the user should be persisting the program each run for longer and just running rand() to get more random numbers. Let me make this clearerIvanna
Well, I did not say you're wrong, just saying don't propose changes like the user should be persisting the program each run for longer. This really depends on the nature of the program, isn't it?Mita
@SouravGhosh That is true, however in truth rand() should not be used for anything except toy programs anyway. I am assuming the user is just trying to learn some basic C and is flexible on how the program works somewhat.Ivanna
hmm.. I thought rand() is reliable when used properly. I'm interested. Can you point me towards some link where I can possibly read about the downside of using rand() in real programs?Mita
@SouravGhosh There are a wide range of sources, but here is a fairly good video on the topic channel9.msdn.com/Events/GoingNative/2013/… In summary, it isnt crypto secure (which may be fine for you), but more importantly, it doesnt have a defined range other than guaranteeing it wont return a number greater than RAND_MAX (which is at least 32767), it doesnt make any guarantees about uniformity so an implementation can have a hugely non uniform distribution. continued -->Ivanna
So in essence there are no safe usages for rand, you cannot take the number as is as there are no guarantees on what the range will be, and you cannot modulo it down to a known range as the result will be non uniform, on top of that the implementation itself may be drastically non uniform.Ivanna
Thanks for the info. Very useful. However the modulo bias you mentioned can be avoided by (re)setting the RAND_MAX, isn' tit?Mita
Let us continue this discussion in chat.Ivanna
S
3

Under Windows you can use GetTickCount() instead of time(). It changes on 50ms interval (if remember correctly).

Sokoto answered 11/6, 2015 at 7:32 Comment(0)
B
2

I had the same issue, I found a easy fix with a high resolution clock.

#include <chrono>
#include <random>

int main() {
    long long t1 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::srand((unsigned int)t1);
}
Boeotia answered 4/1, 2021 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.