I'm looking for a good random algorithm that also works for very small numbers. I have tried several different algos I found on the board and they are all failing when the max value is set 45 (as in my case).

I was thinking about RDTSC and using AL as a random 8-bit value, but maybe it won't work when using it several times per second.
Posted on 2002-10-21 09:04:50 by Delight
Hello Delight,

I demonstrate a very simple example for the generation of random numbers.For this task,I used the rand() function from the Win32 C run-time library (crtdll.dll)

The attachment contains also a very small import library - crtdll.lib for the printf() and rand() functions.

Here is the source code of the CONSOLE application:

.model flat,stdcall
option casemap:none

extern printf:proc
extern rand:proc

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
includelib crtdll.lib

output db 'Random number: %d',13,10,0
counter dd 10

call init
call rand
xor edx,edx
mov ebx,20
div ebx
push edx
push offset output
call printf
add esp,8
dec counter
jnz restart
invoke ExitProcess,0

init proc
local _st:SYSTEMTIME
local temp:dword
invoke GetSystemTime,addr _st
xor eax,eax
mov ax,_st.wMilliseconds
push eax
pop temp
call rand
dec temp
jnz here
init endp
end start

Any value instead of 20 can be typed to change the maximum value returned by the rand() function. :)


Posted on 2002-10-21 13:23:29 by Vortex
Thank you Vortex! I will try it out and see if it fits my needs :)
Posted on 2002-10-21 13:33:41 by Delight
Also consider using the API function CoCreateGuid, it returns a guid which is guaranteed to be unique (and therefore random). If you only want a small number, use a portion of the returned guid.
Posted on 2002-10-22 03:39:02 by sluggy
Thanks sluggy, I'll play with it and see if it's truly random
Posted on 2002-10-22 03:45:13 by Delight