CG, Programming, Drawing, etc.

Menu

IvyBridge RdRand

Introduction

I heard that HW random number generator is implemented in Intel’s CPU since IvyBridge architecture. Today I tried to use it.

How to use?

We can directly access rdrand instruction through assembler but it is easy to use intrinsics offered by the header immintrin.h. The header is same one as AVX intrinsics. For obtaining uniform double random number in the range of [0, 1], we can use the following simple code:

C++

1

2

3

4

5

6

doublerdrand()

{

uint64_tu;

_rdrand64_step(&u);

return(double)u/UINT64_MAX;

}

Experiments

Speed test vs. Mersenne twister

This article says rdrand is capable of 7000M instructions / seconds. How fast is it? I compared the speed to MT(std::random implementation) in a simple experiment. Test code:

Trace paths!!

I added simple modification to smallpt (simply replacing erand48 to the above rdrand function). The source is here. And comparison with MT version (note that erand48 is not used in Windows so I used std::random one instead). Source: MT version and rdrand version.