Below there is an assembly language program that use RDRAND instruction to get a non-zero 16-bits random number, so the generated number is in the 1-65535 range; the number is returned via ERRORLEVEL. Technical details on this program and on the random number generator itself are fully explained in this Intel document. Both this program and the executable .EXE one are included in the .ZIP file.
EDIT 2016/10/21: A small bug in the original program was fixed. Please, delete your RdRand.* files and download the .ZIP file again.
Code: Select all
;RdRand.asm: Return a random number generated by RDRAND CPU instruction
;Antonio Perez Ayala
.model flat, stdcall
option casemap :none
ExitProcess PROTO STDCALL :DWORD
mov eax, 1 ;value for CPUID: returns feature information in ECX
cpuid ;get CPUID value in ECX
mov eax, 0 ;initialize return value = 0
test ecx, 40000000H ;is RDRAND instruction supported? (bit 30)
jz terminate ;no: terminate
mov cl, 10 ;set max. num. of retries
rdrand ax ;get a 16-bits random number in AX
jc terminate ;random number available? return it
dec cl ;decrement retries
jnz getRand ;and go back if not zero
mov eax, -1 ;else: indicate an error condition
push eax ;pass EAX as parameter for ExitProcess
call ExitProcess ;and return it as ERRORLEVEL
The bad news now: as said before, the RDRAND instruction does NOT work in all computers, but just in the most expensive ones. This program does not work in my old-and-cheap laptop, so I have not means to know if RdRand.exe works correctly! If the CPU does not support the RDRAND instruction, RdRand.exe returns 0. I'll appreciate it if you may confirm that RdRand.exe program correctly return random numbers in any computer; please, include the CPU model if you know it.