Question

I've been ripping my hair out for a while now with these random numbers in C++.

In Python, I had the awesome:

random.uniform(0, 1)

Which churned out a new random number each time I called it.

C++ has to have something like this. I Googled for a long time, and found erand48(), which I plan to implement into my raytracer (I'm translating it from Python to C++).

I tried a simple test case, but I was hoping to create a random_uniform() function which always spits out a new random number (using time() isn't going to work AFAICT, as this will be running really quickly)

unsigned short Xi[3] = {0, 1, 27};
std::cout << erand48(Xi);

And the output was (and will be every time I call the program):

0.174529

I tried using the previous output as the new Xi, like this (Xis initial value was defined):

float random_uniform() {
  long generated = erand48(Xi);
  int temp = generated * 1000000;

  unsigned short Xi[3] = {temp - 16, temp - 7, temp - 18};

  return generated;
}

But that doesn't seem like it would generate random enough numbers (and it only spits out 0. I', not sure why...).

Is there any way that I could make a function which spits out a new random number each time?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top