English Amiga Board

Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

Thread Tools
Old 27 August 2017, 22:51   #1
This cat is no more
jotd's Avatar
Join Date: Dec 2004
Location: FRANCE
Age: 50
Posts: 5,793
asm code for a simple random generator


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.

jotd is offline  
Old 27 August 2017, 23:48   #2
Defendit numerus

ross's Avatar
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 51
Posts: 3,522
Hi jotd, the first idea that came in my mind was using CIA TOD.

So googled: "amiga random generator using cia tod" and found

ross is offline  
Old 28 August 2017, 00:06   #3
Join Date: Jul 2008
Location: Sweden
Posts: 2,265
    add.l  d0, d0
    bcc    .1
    eor.l  #1387483827, d0
    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
Leffmann is offline  
Old 28 August 2017, 09:46   #4
son of 68k
meynaf's Avatar
Join Date: Nov 2007
Location: Lyon / France
Age: 48
Posts: 4,359
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) :
 mulu #$a57b,d0
 addi.l #$bb40e62d,d0
 rol.l #6,d0
Something intermediate (period = 2^32-1) :
 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
Something big that passes (all ?) randomness tests :
 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
meynaf is offline  
Old 28 August 2017, 21:49   #5
This cat is no more
jotd's Avatar
Join Date: Dec 2004
Location: FRANCE
Age: 50
Posts: 5,793
that's excellent already. Thanks everyone
jotd is offline  
Old 13 September 2017, 19:01   #6
Registered User

Join Date: Sep 2015
Location: Germany
Posts: 245
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

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
    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
    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
    CNOP 0,2                         ;Word alignment
    DS.W random_values_num
Just play a little bit with the constant values. I use this method to produce random x-values for a horizontal starscrolling.

Last edited by dissident; 13 September 2017 at 23:45.
dissident is offline  
Old 13 September 2017, 21:19   #7
move.l #$c0ff33,throat

StingRay's Avatar
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,664
.rnd	movem.l	d1-d3/a0,-(a7)
	lea	.RNDNUM(pc),a0
	moveq	#8-1,d3
	move.l	(a0),d0
	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

.RNDNUM	dc.l	20170913
StingRay is offline  
Old 15 September 2017, 09:14   #8
Computer Nerd

Thorham's Avatar
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
Originally Posted by jotd View Post
I read that thread http://eab.abime.net/showthread.php?t=71998 but that sounds overkill
That's a hash function, not a PRNG

Anyway, don't underestimate the importance of PRNG quality. For game mechanics a quality PRNG is critically important.
Thorham is offline  

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 06:17.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.08735 seconds with 15 queries