Example of random number generator

Page 3/3
1 | 2 |

By poke-1,170

Paragon (1762)

poke-1,170's picture

20-03-2008, 03:10

speaking of random numbers....

http://youtube.com/watch?v=qjOZtWZ56lc

By poke-1,170

Paragon (1762)

poke-1,170's picture

20-03-2008, 03:10

speaking of numbers....

http://youtube.com/watch?v=qjOZtWZ56lc

By ro

Guardian (4180)

ro's picture

20-03-2008, 08:37

I'd like a number 2, please....

By ARTRAG

Enlighted (6407)

ARTRAG's picture

20-03-2008, 10:57

Well, it generates 7 bit random numbers even if bit 7 stays to 1.

Any idea on how good it is ?

I just incorporated it in my z80 asm random maze generator with mixed results... the mazes look ok (although patterns are visible), but the loops that distribute treasure [making sure there's only 1 treasure per Y tile to avoid too many sprites per row] sometimes gets stuck in an endless (?) loop, waiting for a suitable random number.

Using the high byte (ld A,H) of this function gives very nice results:

SEED: equ rand16 + 1

rand16:
   ld	BC,0
   ld	HL,253
   xor A
   sbc HL,BC
   sbc A,B
   sbc HL,BC
   sbc A,B
   ld	C,A
   sbc HL,BC
   jr	nc,.end
   inc HL
.end
   ld	(SEED),HL ; self modifying code (seed is hardcoded in opcode)
   ret

p/s: this function was ripped of the internet and slightly optimized by me.

Thanks!! This is a true random generator.
Actually my code can fall in "loops" (i.e. bad random sequences) when it is called in a loop itself without branches.
This is due to the fact that R counts the number of executed instructions, so when the loop calling the rand generator
has no branches the counter is increased of a fixed quantity and bad sequences can happen.

I call my rand generator after some large switches so in my case I do not incur in big problems.
Anyway I see it is not very random.

By Fudeba

Expert (113)

Fudeba's picture

20-03-2008, 15:28

A faster uniform generator for 7 bit numbers should be :

rand:
                ld   a,r
...

IMHO seems even more uniform than the first one posted at the beginning of this tread...
Anyone willing confirm my impression?

I believe using R register is a nice thing to get a single random value, given that at least one user interaction was required since R register was reset. You can get random (pseudo, of course) sequences using it several times, but I believe they probably will not be uniform.
For uniform random sequences it is better to use a LFSR function, as the original posted one (very well explained by RicBit and the links he provided). It seems weird, but it really works.

By ARTRAG

Enlighted (6407)

ARTRAG's picture

20-03-2008, 16:00

I'm sure of that. But this is shorter and seems fine for my needs.

rand:       
                ld   a,r
old_rnd:   
                xor  0
                ld   (old_rnd+1),a
                ret

For the 2K competition size wins on efficiency
Wink

By nitrofurano

Champion (294)

nitrofurano's picture

16-08-2013, 12:39

ARTRAG wrote:

Well, it generates 7 bit random numbers even if bit 7 stays to 1.

Any idea on how good it is ?

I just incorporated it in my z80 asm random maze generator with mixed results... the mazes look ok (although patterns are visible), but the loops that distribute treasure [making sure there's only 1 treasure per Y tile to avoid too many sprites per row] sometimes gets stuck in an endless (?) loop, waiting for a suitable random number.

Using the high byte (ld A,H) of this function gives very nice results:

SEED: equ rand16 + 1

rand16:
   ld	BC,0
   ld	HL,253
   xor A
   sbc HL,BC
   sbc A,B
   sbc HL,BC
   sbc A,B
   ld	C,A
   sbc HL,BC
   jr	nc,.end
   inc HL
.end
   ld	(SEED),HL ; self modifying code (seed is hardcoded in opcode)
   ret

p/s: this function was ripped of the internet and slightly optimized by me.

Thanks!! This is a true random generator.
Actually my code can fall in "loops" (i.e. bad random sequences) when it is called in a loop itself without branches.
This is due to the fact that R counts the number of executed instructions, so when the loop calling the rand generator
has no branches the counter is increased of a fixed quantity and bad sequences can happen.

I call my rand generator after some large switches so in my case I do not incur in big problems.
Anyway I see it is not very random.

(sorry about bringing back this thread to activity! Big smile )

about that seed address, where msx bios stores random seeds? i'm asking it because i'm using $f40b (aspct1 - horizontal / vertical aspect for circle command) for that - i'm using it for creating a library for msx version of boriel’s zx-basic compiler, and i think that using "official" variable system area would keep things a bit more organized, and as well that hardcoding doesn't work on code stored in rom (like games in rom cartridges)

the library/msxrnd16.bas file looks like

function msxrnd16() as uinteger:
  '- i don't know where random seed is stored on msx system variables on ram
  '- using ASPCT1 (Horizontal / Vertical aspect for CIRCLE command) at 0xF40B
  '- code based on <a href="http://www.msx.org/forum/development/msx-development/example-random-number-generator?page=1" title="http://www.msx.org/forum/development/msx-development/example-random-number-generator?page=1">http://www.msx.org/forum/development/msx-development/example...</a> from MOA
  asm
    rand16:
    ld BC,($F40B)
    ld HL,253
    xor A
    sbc HL,BC
    sbc A,B
    sbc HL,BC
    sbc A,B
    ld C,A
    sbc HL,BC
    jr nc,rand16end
    inc HL
    rand16end:
    ld ($F40B),HL
    ret
    end asm
  return peek(uinteger,$F40B)
  end function

but i really wanted to use a more appropriated address than 0xF40B....
thanks and cheers! Smile

By nitrofurano

Champion (294)

nitrofurano's picture

16-08-2013, 12:52

(i don't know if this question is silly: how would look like an 8bit and 32bit version of this assembly code? i'm using "ubyte" instead of "uinteger" on library/msxrnd8.bas :S )

Page 3/3
1 | 2 |