flat assembler
Message board for the users of flat assembler.
Index
> Main > random number generator - 8bit - seed based? |
Author |
|
Vasilev Vjacheslav 08 Feb 2008, 09:33
something like this
invoke GetTickCount imul eax,eax,343FDh add eax,269EC3h mov [hSeed],eax shr eax,10h movzx eax,al ; rnd in eax |
|||
08 Feb 2008, 09:33 |
|
SomeoneNew 08 Feb 2008, 10:33
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. |
|||
08 Feb 2008, 10:33 |
|
Madis731 08 Feb 2008, 10:46
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. |
|||
08 Feb 2008, 10:46 |
|
edfed 08 Feb 2008, 12:47
|
|||
08 Feb 2008, 12:47 |
|
r22 08 Feb 2008, 14:58
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 |
|||
08 Feb 2008, 14:58 |
|
edfed 08 Feb 2008, 15:09
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! |
|||
08 Feb 2008, 15:09 |
|
r22 08 Feb 2008, 15:21
edfed wrote: erf. re: edfed Obviously you missed the part where Someonenew wrote:
Your ECX assumption Does Not Compute. |
|||
08 Feb 2008, 15:21 |
|
bitRAKE 08 Feb 2008, 15:27
Code: SEED dd 1 imul eax,[SEED],101b bswap eax rcr eax,3 sbb [SEED],eax Edit: look at the least significant bit - bounces around pretty good. Just counted output bytes and distribution is good. Last edited by bitRAKE on 08 Feb 2008, 15:55; edited 1 time in total |
|||
08 Feb 2008, 15:27 |
|
edfed 08 Feb 2008, 15:46
Quote:
it computes, but as bitrake says, only one reg is needed for pseudo random generation. |
|||
08 Feb 2008, 15:46 |
|
SomeoneNew 09 Feb 2008, 00:00
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
_________________ Im new, sorry if I bothered with any stupid question |
|||
09 Feb 2008, 00:00 |
|
bitRAKE 09 Feb 2008, 00:36
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. |
|||
09 Feb 2008, 00:36 |
|
Vasilev Vjacheslav 10 Feb 2008, 10:47
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 |
|||
10 Feb 2008, 10:47 |
|
edfed 10 Feb 2008, 10:55
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 |
|||
10 Feb 2008, 10:55 |
|
Borsuc 14 Feb 2008, 15:40
xorshift (same thread edfed linked, around page 3). Or simply here.
good, fast, supports large seeds, no multiplications... |
|||
14 Feb 2008, 15:40 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.