Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Is there a reason you're not just using <code>rand()</code>, which is part of the C++ standard library? The function you refer to is part of the Single UNIX Specification, but is probably not portable...</p> <p>The reason you're getting the same value over and over is because you're not seeding the random number generator, so it behaves deterministically. Use <code>srand()</code> or <code>srand48()</code> if you're OK with the UNIX-specific functions and need the 48-bit precision. <strong>Only call it once</strong> at the beginning of your program, and pass it <code>time(NULL)</code>. Don't worry about using <code>time()</code>, since you're only calling this function once, you don't need to worry about always getting the same time value.</p> <p>You may also want to refer to this page about how to take the return value from <code>rand()</code> and scale it to the range you want effectively:</p> <p><a href="http://members.cox.net/deleyd/random/crandom.html" rel="nofollow" title="USING THE C OR C++ rand&#40;&#41; FUNCTION">USING THE C OR C++ rand&#40;&#41; FUNCTION</a></p> <hr> <p>EDIT: Go back and read the docs for <code>erand48</code> again. You'll notice it returns a double in the range [0, 1). You're putting that in a long, which is probably why you're always seeing zero (the value is truncated to an integer). I also think that it's probably updating the values of the <code>unsigned long</code> array for you with the current number in the sequence -- you provide the storage, it uses it to "keep track" of the sequence.</p> <p>Try:</p> <pre><code>unsigned short xsubi[3] = { 0, 1, 27 }; for (int i = 0; i &lt; 10; ++i) { double value = erand48(xsubi); printf("%f\n", value); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload