Message board for the users of flat assembler.
> Windows > GameDev Random Number Generator
I need some ideas for a really fast random number generator. There's bit twister and what not but I already have an algorithm that uses a 64bit seed and outputs random 32bit integers which pass most entropy and other tests better than most known algorithms.
The problem is HOW TO IMPLEMENT IT.
The game engine is Torque and the game is a first person shooter. THere are plenty of C++ classes that give random numbers but they aren't that fast and using inline assembly inside of a C++ class is well a BIG MESS.
I was thinking of a DLL but how would I make it thread safe and easy to use inside of a mostly C++ code base.
Heres the algo if you were curious.
RandInt: .resv equ ebp-8 .high equ ebp+8 push ebp mov ebp,esp sub esp,8 push edx push ebx xor edx,edx movq mm0, qword[seed] ;64bit seed NOT 0 movq qword[.resv],mm0 mov ecx,dword[.resv+4] ;;2nd half of seed mov eax,dword[.resv] ;;1st half of seed shld eax,ecx,1 ;;shift in 1 bit from 2nd half to first mov dword[.resv],eax ;;update seed in local memory adc ecx,0 ;;add the carry flag ror ecx,3 ;;odd roll-right bswap ecx ;;make more random mov dword[.resv+4],ecx ;;update 2nd half of seed in local memory movq mm0, qword[.resv] movq qword[seed],mm0 mov ecx,dword[.high] ;;process high cmp ecx,-1 je .rdone ;;if high = FFFFFFFFh retn rnd dword test ecx,ecx jz .rdone ;;if high = 0 retn rnd dword inc ecx div ecx mov eax,edx .rdone: pop ebx pop edx add esp,8 mov esp,ebp pop ebp retn 4
|02 Apr 2005, 03:09||
i have done a simple but a working radnom generator
This is MASM
lods Byte Ptr [esi]
stos Byte Ptr [edi]
invoke wsprintf,ADDR lpBuffer,ADDR Format,[lpBuffer]
invoke SetDlgItemText,Handle,1005,ADDR lpBuffer
you set lpBuffer a number like the handle of the window you have and then run the loop and you have the random number in lpBuffer after the loop
|19 May 2005, 10:40||
and how to set a range?
Microsoft: brings power of yesterday to computers of today.
|19 May 2005, 11:59||
in ecx? you can set how many times i should do the loop.
but you can not do more than 4bytes now so you can modify it to the range you want to have or i can do it.
|19 May 2005, 12:15||
well basically i think random number generator is the same for all os es, only the parameter passing differs,
that's why i was thinking about moving random number generators from dos to main, but because of the slight difference and the moderate size of the threads it seemed inneccesary.
here are 2 threads about random number generators
Small random integer generator in Main
Rando number generator in DOS
|19 May 2005, 14:53||
Here is a link to agner fogs random number generator page. Here you'll find different types of random number generators, both integer and floating point, along with explanations as to how they work. Some are in C, some use assembly (MASM).
|24 May 2005, 17:36||
"rdtsc" mnemonic return 64 bits value and ticking only 6-11 times(it's contingent from processor). I don't know is it possible to return a random value faster. If you want how I use it - look at:
|26 May 2005, 21:59||
< Last Thread | Next Thread >
Copyright © 1999-2020, Tomasz Grysztar.
Powered by rwasa.