flat assembler
Message board for the users of flat assembler.

Index > Windows > GameDev Random Number Generator

Author
Thread Post new topic Reply to topic
r22



Joined: 27 Dec 2004
Posts: 805
r22
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.
Code:
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
    
Post 02 Apr 2005, 03:09
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
lilljocke



Joined: 28 Dec 2004
Posts: 34
Location: Sweden
lilljocke
i have done a simple but a working radnom generator

This is MASM
CODE:

lea esi,lpBuffer
mov edi,esi
mov ebx,[edi]
mov ecx,4

Loop1:
lods Byte Ptr [esi]
rol ebx,1
add eax,ebx
ror edx,cl
add ah,dl
xor al,bh
stos Byte Ptr [edi]
loopd Loop1

invoke wsprintf,ADDR lpBuffer,ADDR Format,[lpBuffer]
invoke SetDlgItemText,Handle,1005,ADDR lpBuffer
Ret

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
Post 19 May 2005, 10:40
View user's profile Send private message Visit poster's website Reply with quote
HarryTuttle



Joined: 26 Sep 2003
Posts: 211
Location: Poland
HarryTuttle
and how to set a range?

_________________
Microsoft: brings power of yesterday to computers of today.
Post 19 May 2005, 11:59
View user's profile Send private message Reply with quote
lilljocke



Joined: 28 Dec 2004
Posts: 34
Location: Sweden
lilljocke
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.
Post 19 May 2005, 12:15
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
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
Post 19 May 2005, 14:53
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
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).
HTTP: http://www.agner.org/random/
Post 24 May 2005, 17:36
View user's profile Send private message Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov
"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:
http://board.flatassembler.net/download.php?id=1480
Post 26 May 2005, 21:59
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.