English Amiga Board


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

 
 
Thread Tools
Old 27 August 2017, 23:51   #1
jotd
Cat freak
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 46
Posts: 1,924
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
jotd is offline  
AdSense AdSense  
Old 28 August 2017, 00:48   #2
ross
Omnia fert aetas

ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 47
Posts: 513
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
ross is offline  
Old 28 August 2017, 01:06   #3
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,020
Simpler:
Code:
    add.l  d0, d0
    bcc    .1
    eor.l  #1387483827, d0
.1
Better:
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
Leffmann is offline  
Old 28 August 2017, 10:46   #4
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 44
Posts: 2,413
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
Something intermediate (period = 2^32-1) :
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
Something big that passes (all ?) randomness tests :
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
meynaf is offline  
Old 28 August 2017, 22:49   #5
jotd
Cat freak
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 46
Posts: 1,924
that's excellent already. Thanks everyone
jotd is offline  
Old 13 September 2017, 20:01   #6
dissident
Registered User

 
Join Date: Sep 2015
Location: Germany
Posts: 74
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
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; 14 September 2017 at 00:45.
dissident is offline  
Old 13 September 2017, 22:19   #7
StingRay
move.l #$c0ff33,throat

StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 5,567
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
StingRay is offline  
Old 15 September 2017, 10:14   #8
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 41
Posts: 2,964
Quote:
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  
AdSense AdSense  
 


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 2 25 September 2016 03:41
VBCC 09d - bug in code generator ? Asman Coders. C/C++ 7 04 January 2016 14:46
Problems with a little ASM code VoltureX Coders. General 7 12 December 2011 14:10
random number generation (in asm) meynaf Coders. General 183 29 November 2010 20:48
Help needed!!Random octal numbers generator(asm) sheryn88 Coders. General 6 01 August 2010 08: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 04:47.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Page generated in 0.16319 seconds with 14 queries