flat assembler
Message board for the users of flat assembler.

 Index > DOS > Random numbers Goto page Previous  1, 2
Author
 Thread
MCD

Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 22 Sep 2004, 19:29
The RDTSC-version is the smallest, easiest and quickest one from all.

If you want doing it with a mathematical formula, try this optimized chaotic logistic growth model formula:

Xn+1 = (4 - Xn) * Xn

That's all!

Where X0 should be between 0 and 4 and should not be an integer number

=> 0 < X0 < 4 & ! (X0 IN IntegerNumber)

I have both integer and floating point ) version somewhere around, but I messed them up and don't find them anymore.
22 Sep 2004, 19:29
BreakDebugJeff

Joined: 22 Oct 2004
Posts: 1
Location: Ocean Shores, WA
BreakDebugJeff 09 Nov 2004, 10:51
I am new here, but I saw another user post the RDTSC instruction in this colum, and thought that I would add my two cents:

Code:
```inline int randrnd(int max)
{
int a;
_asm
{
rdtsc   ; call read time-stamp counter instruction
; returns timer value in edx:eax
cdq     ; convert double word to quad word
; edx:eax <-- sign extension of EAX
idiv max   ; divide edx:eax by max integer
; stores result in EAX <- Quotient, EDX <- Remainder
; EDX = edx:eax % max
cmp edx, 0  ; compare value to zero?
jnl after       ; jump if not less than to after: label
neg edx      ; otherwise, change sign to positive
after:
mov a, edx  ; store random number in a
}
return(a);      // return random number
}
```

This is an inline assembly program function that works in Visual C++.NET

Let me know what you think.

Jeff
09 Nov 2004, 10:51
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 12:24
Hy,
wel, it doesn't seem to be a random to me, however i 'd do this instead:

Code:
```inline int randrnd(int max)
{
int a;
_asm
{
rdtsc ; call read time-stamp counter instruction
; returns timer value in edx:eax
cdq ; convert double word to quad word
; edx:eax <-- sign extension of EAX
idiv max ; divide edx:eax by max integer
; stores result in EAX <- Quotient, EDX <- Remainder
; EDX = edx:eax % max
or edx,edx ;cmp edx, 0 ; compare value to zero?
jns after ;jnl after ; jump if not less than to after: label
neg edx ; otherwise, change sign to positive
after:
mov a, edx ; store random number in a
}
return(a); // return random number
}
```
09 Nov 2004, 12:24
beppe85

Joined: 23 Oct 2004
Posts: 181
beppe85 09 Nov 2004, 13:05
If you need a non-negative integer, treat it as such!

Code:
```rdtsc
xor     edx, edx ; non-negative quad-word
div     max      ; hope it work so
```

Not tested, but I believe it does the same thing. Note that it returns a number 0 <= n < max, ie. the max is not the max, it's the number at the right of the real max!
09 Nov 2004, 13:05
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 16:13
btw.: here 's my latest random number generator, better then the ones i wrote recently

Code:
```org 256

call set320x200x256
call initrandomz

mainloop:

mov ecx,320
call randomz3
mov bx,ax

mov ecx,200
call randomz3
mov cx,ax

push ecx
mov ecx,256
call randomz3
pop ecx

call putpixel320x200x256 ; al=color bx=x cx=y

push eax
call bkeycheck
cmp al,27
pop eax
jnz mainloop

call set80x25t

int 20h

initrandomz: ; modifies edx, eax
rdtsc
ret

randomz3: ; Z3 ecx=range transparent
push ebx  ; number returned is:  0 <= n < ecx
push edx
mov ebx,eax
rdtsc

mov dx,cx

mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,\$f
rol bx,cl
btc bx,cx
xor bx,ax
ror eax,16
xor bx,ax
ror eax,16
mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,\$f
ror bx,cl
btc bx,cx

mov cl,bl
.minorloop:
rol eax,cl
and cx,\$f
btc ax,cx
loopw .minorloop
mov cl,bh
.majorloop:
ror eax,cl
and cl,\$f
btc ax,cx
loopw .majorloop
mov cx,dx

mul ecx
mov eax,edx
pop edx
pop ebx
ret

putpixel320x200x256: ; al=color, bx=x, cx=y
push    es
push    \$A000
pop     es
mov     di,cx   ; y
shl     cx,2
add     di,cx   ; 5y
shl     di,6    ; 320y
add     di,bx
stosb
pop     es
ret

bkeycheck:  ;returns: AH = BIOS scan code AL = ASCII character note: enhanced
mov ah,0x11 ;Return: ZF set if no keystroke available, ZF clear if keystroke available
int 0x16    ;only checks buffer without removing key
ret

set320x200x256:
mov ax,\$13
int \$10
ret

set80x25t:
mov ax,\$03
int \$10
ret
```
09 Nov 2004, 16:13
Bitdog

Joined: 18 Jan 2004
Posts: 97
Bitdog 12 Nov 2004, 02:37
I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc
0-255, then alph organize the text file output 0-255
I found that the nicest looking, intelegent, best promoted RANDOM number gen was usually the worst. They would miss numbers (lots of them)
and would have one number get an unusually high number of hits
compaired to other numbers. It's suspose to be an even amount of hits
for each number when the generator is called repeatedly alot.
Any way, since most programs that use a RAND GEN work in a loop,
which calls the generator on consistant intervals, the save/count loop
seemed to act much like a commonly used program.

Well, it's just some thoughts anyway.

Bitdog
12 Nov 2004, 02:37
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 12 Nov 2004, 03:09
Bitdog wrote:
I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc
0-255, then alph organize the text file output 0-255
I found that the nicest looking, intelegent, best promoted RANDOM number gen was usually the worst. They would miss numbers (lots of them)
and would have one number get an unusually high number of hits
compaired to other numbers. It's suspose to be an even amount of hits
for each number when the generator is called repeatedly alot.
Any way, since most programs that use a RAND GEN work in a loop,
which calls the generator on consistant intervals, the save/count loop
seemed to act much like a commonly used program.

Well, it's just some thoughts anyway.

Bitdog

Bitdog,
is it air you breathing?

somtimes things are not what they seem to be.
12 Nov 2004, 03:09
Bitdog

Joined: 18 Jan 2004
Posts: 97
Bitdog 14 Nov 2004, 16:45
So what's wrong with checking the output of a Rand Gen ?
14 Nov 2004, 16:45
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 15 Nov 2004, 01:37
Nothing wrong , test it excessively.
i tried to plot random xmax ymax color pixels to 1024x768 24 bit and it looked random, and no white spots or somethin'
so its great for me. and it has MIPSSS!!
not like storing large arrays of previus numbers and doing whit whatever matrix transformations.
15 Nov 2004, 01:37
edfed

Joined: 20 Feb 2006
Posts: 4280
Location: Now
edfed 17 Oct 2007, 00:17
how can we use these random generators to have a simple function

it need to be really fast and random

it is a background program(in timer interrupt also)
and the function is called by
[code]
call rnd32
call rnd16
call rnd8
call rndfp

and all returns a value in eax,ax,al or st0

no?

one thing: MATRIX, I've tried your rnd, it is not very rnd and if ecx is too big, it crash
and it is not the faster
17 Oct 2007, 00:17
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 28 Oct 2007, 21:16
sorry for late reply,
though it won't be useful either,

i wanted to make a simple range setting option using ECX as maximum and 0 for minimum,
if you have other ideas that are better i'm interested too.

the random generated by the function is 32bit without the maximum setting at the end.

if testing it with putpixel, do not set above the limits, or the putpixel will overflow and write below the video memory buffer.
28 Oct 2007, 21:16
edfed

Joined: 20 Feb 2006
Posts: 4280
Location: Now
edfed 28 Oct 2007, 21:47
you just have to look at /projects and ideas/RANDOM
with many guys, we have discuss about simples randoms functions that are finally usefull for all X86 based systems

needs some (not really need ) optimisations
28 Oct 2007, 21:47
MCD

Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 29 Oct 2007, 11:10
I like it. I used to use a similar one
29 Oct 2007, 11:10
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page Previous  1, 2

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum

Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.