News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

question about rdrand

Started by xanatose, September 16, 2015, 08:41:29 AM

Previous topic - Next topic

xanatose

Does anyone knows what would be the best way to convert a random generated from


rdrand edx


to a real8 between 0 and 1?

When using rand() (in C) is something like this:

(double)rand() / (double)RAND_MAX


But since I do not know the maximum value for rdrand is difficult to do the same thing.

jj2007

Quote from: xanatose on September 16, 2015, 08:41:29 AM
Does anyone knows what would be the best way to convert a random generated from


rdrand edx


to a real8 between 0 and 1?

rdrand edx
push edx
fild dword ptr [esp]
fmul FP4(4294967295.0)
fstp SomeReal


Can't test it because my cpu doesn't know rdrand.

dedndave

i think you want to use FDIV, not FMUL   :P

although, if you want to use FMUL (which may be faster)
you could multiply it by a real constant that is 1/4294967295.0 (i would use a REAL10 constant)

r10Constant REAL10 2.3283064370807973754314699618685e-10

Mikl__


dedndave

i haven't had the chance to write any 64-bit code, yet
from what i've seen, i'm in no hurry, either - lol

ok - this is the 64-bit forum, so i guess we should have used SSEx code   :P

dedndave

i should mention that the constants Jochen and I supplied will yield results from 0.0 to +1.0, inclusive
if you don't want the result to include +1.0, use 1/4294967296

MichaelW

I'm assuming that you want a number in the interval [0, 1).

This is how the FreeBASIC RTL does it:

static double hRnd_FAST ( float n )
{
/* return between 0 and 1 (but never 1) */
/* Constants from 'Numerical recipes in C' chapter 7.1 */
if( n != 0.0 )
iseed = ( ( 1664525 * iseed ) + 1013904223 );

return (double)iseed / (double)4294967296ULL;
}


The above is for the fast version, but the default Mersenne Twister version uses essentially the same conversion code.
Well Microsoft, here's another nice mess you've gotten us into.

xanatose

Quote from: Mikl__ on September 16, 2015, 01:24:16 PM
but where is 64 Bit Assembler?
My mistake it should have read.

rdrand rdx


:) As I am dealing with 64 bit numbers.