flat assembler
Message board for the users of flat assembler.

Index > Main > random number generator - 8bit - seed based?

Author
Thread Post new topic Reply to topic
SomeoneNew



Joined: 12 Aug 2006
Posts: 54
SomeoneNew
Hi, I'm looking for an efficient, seed-based random number generator that outputs 8bit data (but takes 32bit data as a seed).

I'd like some info on the subject, perhaps a sample if someone is kind enough to share Smile

I don't need a magic attempt of true randomness, just something to work and learn from.


Thanks Smile

PS: or a generator that outputs 32bit on an 8bit range, that'd be as cool. Point is, I need to be able to reproduce the results exactly by seed at the end...

_________________
Im new, sorry if I bothered with any stupid question Smile
Post 08 Feb 2008, 08:39
View user's profile Send private message Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
something like this

invoke GetTickCount
imul eax,eax,343FDh
add eax,269EC3h
mov [hSeed],eax
shr eax,10h
movzx eax,al

; rnd in eax
Post 08 Feb 2008, 09:33
View user's profile Send private message Reply with quote
SomeoneNew



Joined: 12 Aug 2006
Posts: 54
SomeoneNew
are you using gettickcount as your random source? why not rdtsc in that case?, I need speed and good distribution yet I must be able to reproduce the same output via seed, it's for an experiment I'm conducting.

thanks.
Post 08 Feb 2008, 10:33
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
Code:
SEED equ 1234h
mov eax,SEED
mov ebx,not SEED
rol eax,3
ror ebx,13
add eax,ebx
adc eax,0
    

I haven't conducted any tests on this like "is it reversible?"; "is it spreaded enough?" and that sort, but with multiplication, sometimes bits tend to get lost - with rotates and addition, they usually stay there.
Post 08 Feb 2008, 10:46
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
Post 08 Feb 2008, 12:47
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Code:
.data
SEED dd 1a8b3f57h ;;this will change/ be updated with every call rnd8

.code
;;example calling
RDTSC ;;read the clock timer on the CPU
mov [SEED],eax ;;set a new seed
call rnd8
cinvoke printf, "1 Random 8bits: %X", eax
call rnd8
cinvoke printf, "2 Random 8bits: %X", eax
call rnd8
cinvoke printf, "3 Random 8bits: %X", eax

;;function
rnd8:
MOV eax,[SEED]
MOV ecx,eax ;;copy
SHL eax,1 ;;shift out top bit
ADC ecx,71ae00h ;;add the carry bit and a constant
;;the constant repairs the zero seed = zero output case (hopefully)
SHL ecx,7 ;;shift out top 7 bits (7 is a PRIME number)
SHR eax,11 ;;shift out bottom 11 bits (11 is a PRIME number)
XOR eax,ecx ;;scramble the bits further using exclusive OR
BSWAP eax ;;reverse the byte order to extend the period 
;;(PERIOD: number of calls before the random sequence repeats itself)
MOV [SEED],eax ;;use this as the next iteration of the seed, ie seed.2
AND eax,0FFh ;;mask just the last 8bits 1byte
RET 0
    


Last edited by r22 on 08 Feb 2008, 15:10; edited 1 time in total
Post 08 Feb 2008, 14:58
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
erf.
this is wasty.
Code:
.data 
SEED dd 1a8b3f57h ;;this will change/ be updated with every call rnd8 
.code 
;;example calling 
call GetTickCount 
call rnd8 
cinvoke printf, "1 Random 8bits: %X", eax 
call rnd8 
cinvoke printf, "2 Random 8bits: %X", eax 
call rnd8 
cinvoke printf, "3 Random 8bits: %X", eax 

;;function 
rnd8: 
MOV eax,[SEED] 
ADD ecx,eax ;;add eax to the unknown ecx state! randow like 
ROR eax,5 ;;rotate 5, it's still a pseudo random 
XOR eax,ecx ;;xor , increase randomness 
BSWAP eax ;;reverse the byte order to extend the period  
MOV [SEED],eax ;;use this as the next iteration of the seed, ie seed.2 
AND eax,0FFh ;;mask just the last 8bits 1byte 
RET 
    

this is better!
Post 08 Feb 2008, 15:09
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
edfed wrote:
erf.
this is wasty.
Code:
;;function 
rnd8: 
MOV eax,[SEED] 
ADD ecx,eax ;;add eax to the unknown ecx state! randow like 
...
    

this is better!

re: edfed
Obviously you missed the part where
Someonenew wrote:

PS: or a generator that outputs 32bit on an 8bit range, that'd be as cool. Point is, I need to be able to reproduce the results exactly by seed at the end...

Your ECX assumption Does Not Compute.
Post 08 Feb 2008, 15:21
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2935
Location: vpcmipstrm
bitRAKE
Code:
SEED dd 1

imul eax,[SEED],101b
bswap eax
rcr eax,3
sbb [SEED],eax    
...very few instructions with only one register use - mix in with other code or use multiple SEEDs/PRNs. It does produce the full range of byte values in AL, but distribution hasn't been checked. SEED should never be zero - which ONLY happens when it starts as zero!

Edit: look at the least significant bit - bounces around pretty good. Cool Just counted output bytes and distribution is good.


Last edited by bitRAKE on 08 Feb 2008, 15:55; edited 1 time in total
Post 08 Feb 2008, 15:27
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
Quote:

Your ECX assumption Does Not Compute.

it computes, but as bitrake says, only one reg is needed for pseudo random generation.
Post 08 Feb 2008, 15:46
View user's profile Send private message Visit poster's website Reply with quote
SomeoneNew



Joined: 12 Aug 2006
Posts: 54
SomeoneNew
Thanks guys, I'll be taking a closer look on the code. However for what I could test right now they didn't serve my purpose Sad

_________________
Im new, sorry if I bothered with any stupid question Smile
Post 09 Feb 2008, 00:00
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2935
Location: vpcmipstrm
bitRAKE
There are several good examples in this thread which seem to cover your specific request. What exactly is lacking? I'm having difficultly understanding your needs.

edfed has linked to another random number discussion with further examples both fast and high quality randomness. With cellular automata the distribution can be talored to anything desired.
Post 09 Feb 2008, 00:36
View user's profile Send private message Visit poster's website Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
Quote:
are you using gettickcount as your random source? why not rdtsc in that case?, I need speed and good distribution yet I must be able to reproduce the same output via seed, it's for an experiment I'm conducting.


not so big difference, just replace GetTickCount with rdtsc

hSeed = 123

rdtsc
imul eax,eax,343FDh
add eax,269EC3h
mov [hSeed],eax
shr eax,10h
movzx eax,al


Last edited by Vasilev Vjacheslav on 10 Feb 2008, 10:57; edited 1 time in total
Post 10 Feb 2008, 10:47
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
try, try and try.
if you try many solutions, you'll find a random algo, that is good at random...
Code:
random1:
rdtsc
add eax,edx
ror eax,7
xor eax,edx
ret

random2:
mov eax,[seedl]
mov edx,[seedh]
add ebx,eax
add ecx,edx
ror eax,19
xor eax,ecx
add eax,edx
sub eax,ebx
ret

random3:
mov eax,[eax]
mov eax,[eax]
mov eax,[eax]
mov eax,[eax]
mov eax,[eax]
ret
    

etc, etc...
ret
Post 10 Feb 2008, 10:55
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
xorshift (same thread edfed linked, around page 3). Or simply here.

good, fast, supports large seeds, no multiplications... Very Happy
Post 14 Feb 2008, 15:40
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. Also on YouTube, Twitter.

Website powered by rwasa.