27 August 2017, 22:51 | #1 |
This cat is no more
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,170
|
asm code for a simple random generator
Hi,
I read that thread http://eab.abime.net/showthread.php?t=71998 but that sounds overkill Can someone share some simple random generator asm code? using hardware or not, I don't care. thanks |
27 August 2017, 23:48 | #2 |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Hi jotd, the first idea that came in my mind was using CIA TOD.
So googled: "amiga random generator using cia tod" and found http://eab.abime.net/showthread.php?t=53462 Regards, ross |
28 August 2017, 00:06 | #3 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Simpler:
Code:
add.l d0, d0 bcc .1 eor.l #1387483827, d0 .1 Code:
move.l d0, d1 lsl.l #7, d1 eor.l d1, d0 move.l d0, d1 lsr.l #2, d1 eor.l d1, d0 move.l d0, d1 lsr.l #7, d1 eor.l d1, d0 |
28 August 2017, 09:46 | #4 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
If you don't care about using hardware then i guess this is a "true" generator and not a "pseudo" one which would give same sequence for same initial seed.
What is acceptable depends on the application. Do you favor speed over randomness quality or the other way around ? Can it be 020+ code ? Something simple (repeats results after a few thousands values) : Code:
mulu #$a57b,d0 addi.l #$bb40e62d,d0 rol.l #6,d0 Code:
move.l seed,d0 move.l d0,d2 move.l d0,d1 lsl.l #8,d1 lsl.l #5,d1 eor.l d1,d0 move.l d0,d1 swap d1 lsr.l #1,d1 eor.w d1,d0 move.l d0,d1 lsl.l #5,d1 eor.l d1,d0 move.l d0,seed add.l d2,d0 ; return d0 Code:
movem.l seed,d0/d4 move.w $dff006,d5 swap d5 move.b $bfe801,d7 lsl.w #8,d7 move.b $bfd800,d7 swap d7 move.w $dff014,d7 move.b $bfe601,d5 lsl.w #8,d5 move.b $bfe701,d5 add.l d5,d4 addx.l d7,d0 move.l #$59fa769f,d5 move.l #$96e94329,d7 move.l d0,d6 mulu.l d7,d2:d6 mulu.l d4,d3:d5 mulu.l d7,d0:d4 add.l d5,d6 add.l d6,d0 add.l d4,d0 move.l #$4a5be021,d7 add.l #$afe70c1d,d0 addx.l d7,d4 rol.l d3,d0 lsr.w #5,d3 ror.l d3,d4 movem.l d0/d4,seed ; return d0 |
28 August 2017, 21:49 | #5 |
This cat is no more
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,170
|
that's excellent already. Thanks everyone
|
13 September 2017, 19:01 | #6 |
Registered User
Join Date: Sep 2015
Location: Germany
Posts: 256
|
Try this, jotd. It’s a very simple formula if you want to produce random values within a certain range. The rest of a division is the random value:
f(x+1)=[(f(x)*a)+b]/mod m Code:
const_a EQU 467 ;Just some example values const_b EQU 2048 random_max_value EQU 336 random_values_num EQU 128 CNOP 0,4 ;Longword alignment get_random_values lea random_values_tab(pc),a0 ;Table to store your random values move.w $dff006,d0 ;f(x)=VHPOSR=start value move.w #const_a,d1 move.l #const_b,d2 move.w #random_max_value,d3 moveq #random_values_num-1,d7 ;The number of values you need random_values_loop mulu.w d1,d0 ;f(x)*a add.l d2,d0 ;(f(x)*a)+b divu.w d3,d0 ;f(x+1)=[(f(x)*a)+b]/mod m swap d0 ;Rest of division move.w d0,(a0)+ ;Store the value dbf d7,random_values_loop rts CNOP 0,2 ;Word alignment random_values_tab DS.W random_values_num Last edited by dissident; 13 September 2017 at 23:45. |
13 September 2017, 21:19 | #7 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
Code:
.rnd movem.l d1-d3/a0,-(a7) lea .RNDNUM(pc),a0 moveq #8-1,d3 move.l (a0),d0 .rndloop move.b d0,d1 move.b d0,d2 lsr.b #3,d1 lsr.b #1,d2 eor.b d1,d2 lsr.b #1,d2 roxr.l #1,d0 move.l d0,(a0) dbf d3,.rndloop movem.l (a7)+,d1-d3/a0 rts .RNDNUM dc.l 20170913 |
15 September 2017, 09:14 | #8 | |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,753
|
Quote:
Anyway, don't underestimate the importance of PRNG quality. For game mechanics a quality PRNG is critically important. |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Blitz Seed Random Number Generator | Ze Emulatron | Coders. Blitz Basic | 3 | 26 November 2017 11:08 |
VBCC 09d - bug in code generator ? | Asman | Coders. C/C++ | 7 | 04 January 2016 13:46 |
Problems with a little ASM code | VoltureX | Coders. General | 7 | 12 December 2011 13:10 |
random number generation (in asm) | meynaf | Coders. General | 183 | 29 November 2010 19:48 |
Help needed!!Random octal numbers generator(asm) | sheryn88 | Coders. General | 6 | 01 August 2010 07:19 |
|
|