Author Topic: question about rdrand  (Read 3034 times)

xanatose

  • Member
  • ***
  • Posts: 398
question about rdrand
« on: September 16, 2015, 08:41:29 AM »
Does anyone knows what would be the best way to convert a random generated from

Code: [Select]
rdrand edx

to a real8 between 0 and 1?

When using rand() (in C) is something like this:
Code: [Select]
(double)rand() / (double)RAND_MAX

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

jj2007

  • Member
  • *****
  • Posts: 10537
  • Assembler is fun ;-)
    • MasmBasic
Re: question about rdrand
« Reply #1 on: September 16, 2015, 09:34:06 AM »
Does anyone knows what would be the best way to convert a random generated from

Code: [Select]
rdrand edx

to a real8 between 0 and 1?

Code: [Select]
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

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: question about rdrand
« Reply #2 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)

Code: [Select]
r10Constant REAL10 2.3283064370807973754314699618685e-10

Mikl__

  • Member
  • *****
  • Posts: 1008
Re: question about rdrand
« Reply #3 on: September 16, 2015, 01:24:16 PM »
but where is 64 Bit Assembler?

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: question about rdrand
« Reply #4 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

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: question about rdrand
« Reply #5 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

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: question about rdrand
« Reply #6 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:
Code: [Select]
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

  • Member
  • ***
  • Posts: 398
Re: question about rdrand
« Reply #7 on: September 22, 2015, 09:31:02 AM »
but where is 64 Bit Assembler?
My mistake it should have read.
Code: [Select]
rdrand rdx


:) As I am dealing with 64 bit numbers.