0

I'm using Xcode as my IDE on macOS for some simple pure-C code. The code uses stdlib's rand. A former thread here suggested calling rand twice near the start of main to better randomize it. So I added this code:

// seed the random with the provided number or randomize it based on the time
if (random_seed > -1)
  srand(random_seed);
else
  srand((unsigned int)time(NULL) | (getpid() << 8));

// now call rand to prime the pump
double pump = rand();
pump = rand();

This causes clang to complain that "Value stored to 'pump' during its initialization is never read", which is the entire point. I thought I could fix this by:

rand();
rand();

But then I get the complaint that "Return value of function rand() is not used". So I need to do something with the value to suppress the message(s).

Is there a way to fix this in code? It's been used on macOS, Win and Linuxen, so I'd like to avoid anything clang/Xcode/mac-ish or using the pre-processor. Is there some canonical method in C?

Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • You already fudged the seed with the pid, the "pump" is pointless (given how rand is deterministic, I really don't see how that "pump" could ever help, unless it was called a random number of times - but you need a good random number to do that which you don't have). – Mat Jul 19 '23 at 14:59
  • It is not, click through to the link thread and you'll see why. I was getting the exact same sequences when running small programs that exited quickly. This code fixes this. – Maury Markowitz Jul 19 '23 at 15:24

0 Answers0