The MASM Forum

64 bit assembler => 64 bit assembler. Conceptual Issues => Topic started by: xanatose on September 16, 2015, 08:41:29 AM

Title: question about rdrand
Post by: 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?

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.
Title: Re: question about rdrand
Post by: jj2007 on September 16, 2015, 09:34:06 AM
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.
Title: Re: question about rdrand
Post by: dedndave on September 16, 2015, 09:36:41 AM
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
Title: Re: question about rdrand
Post by: Mikl__ on September 16, 2015, 01:24:16 PM
but where is 64 Bit Assembler?(http://www.cyberforum.ru/images/smilies/be.gif)
Title: Re: question about rdrand
Post by: dedndave on September 16, 2015, 03:11:40 PM
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
Title: Re: question about rdrand
Post by: dedndave on September 16, 2015, 03:13:56 PM
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
Title: Re: question about rdrand
Post by: MichaelW on September 17, 2015, 04:32:57 PM
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.
Title: Re: question about rdrand
Post by: xanatose on September 22, 2015, 09:31:02 AM
Quote from: Mikl__ on September 16, 2015, 01:24:16 PM
but where is 64 Bit Assembler?(http://www.cyberforum.ru/images/smilies/be.gif)
My mistake it should have read.

rdrand rdx


:) As I am dealing with 64 bit numbers.