Example of random number generator

Página 3/4
1 | 2 | | 4

Por poke-1,170

Paragon (1769)

Imagen del poke-1,170

20-03-2008, 03:10

speaking of random numbers....

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

Por poke-1,170

Paragon (1769)

Imagen del poke-1,170

20-03-2008, 03:10

speaking of numbers....

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

Por ro

Scribe (4745)

Imagen del ro

20-03-2008, 08:37

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

Por ARTRAG

Enlighted (6862)

Imagen del ARTRAG

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.

Por Fudeba

Expert (113)

Imagen del Fudeba

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.

Por ARTRAG

Enlighted (6862)

Imagen del ARTRAG

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

Por nitrofurano

Champion (301)

Imagen del nitrofurano

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

Por nitrofurano

Champion (301)

Imagen del nitrofurano

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 )

Por aoineko

Hero (520)

Imagen del aoineko

19-12-2021, 13:27

ARTRAG wrote:

A faster uniform generator for 7 bit numbers should be :

rand:
                ld   a,r
                ld   b,a
                ld   a,(old_rnd)
                xor  b
                ld   (old_rnd),a
                ret

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

A "little" late reply, but I have included this version in my test program for the distribution of pseudo-random number generators and the results are very good.

I'm using a fairly close version that I thought was better quality, but in the end I feel like they have the same entropy and the result changes a bit depending on the changes I make in my program (since the R register value depends on the code).

Here is the heatmap for 32765 draws:

You can find other RNG algorithms heatmaps on the MSXVillage (I wanted to put the other images here, but I can't post them on this forum without copying them in a https :-/) : http://msxvillage.fr/forum/topic.php?pt=2&id=1524#m89122

Por Grauw

Ascended (10617)

Imagen del Grauw

19-12-2021, 14:41

Have you tested this method with the R register on the turboR in R800 mode as well?

Página 3/4
1 | 2 | | 4