added math.randrange() and math.randrangew()

This commit is contained in:
Irmen de Jong
2024-02-10 22:16:13 +01:00
parent 4dc50cb551
commit 8b8caa1c2e
4 changed files with 70 additions and 10 deletions

View File

@@ -75,7 +75,7 @@ _sinecosR8 .char trunc(127.0 * sin(range(180+45) * rad(360.0/180.0)))
}} }}
} }
asmsub rnd() -> ubyte @A { asmsub rnd() clobbers(Y) -> ubyte @A {
%asm {{ %asm {{
jmp math.randbyte jmp math.randbyte
}} }}
@@ -87,6 +87,24 @@ _sinecosR8 .char trunc(127.0 * sin(range(180+45) * rad(360.0/180.0)))
}} }}
} }
sub randrange(ubyte n) -> ubyte {
; -- return random number uniformly distributed from 0 to n-1 (compensates for divisibility bias)
cx16.r0H = 255 / n * n
do {
cx16.r0L = math.rnd()
} until cx16.r0L < cx16.r0H
return cx16.r0L % n
}
sub randrangew(uword n) -> uword {
; -- return random number uniformly distributed from 0 to n-1 (compensates for divisibility bias)
cx16.r1 = 65535 / n * n
do {
cx16.r0 = math.rndw()
} until cx16.r0 < cx16.r1
return cx16.r0 % n
}
asmsub rndseed(uword seed1 @AY, uword seed2 @R0) clobbers(A,Y) { asmsub rndseed(uword seed1 @AY, uword seed2 @R0) clobbers(A,Y) {
; -- set new pseudo RNG's seed values. Defaults are: $00c2, $1137 ; -- set new pseudo RNG's seed values. Defaults are: $00c2, $1137
%asm {{ %asm {{

View File

@@ -176,6 +176,24 @@ math {
}} }}
} }
sub randrange(ubyte n) -> ubyte {
; -- return random number uniformly distributed from 0 to n-1 (compensates for divisibility bias)
cx16.r0H = 255 / n * n
do {
cx16.r0L = math.rnd()
} until cx16.r0L < cx16.r0H
return cx16.r0L % n
}
sub randrangew(uword n) -> uword {
; -- return random number uniformly distributed from 0 to n-1 (compensates for divisibility bias)
cx16.r1 = 65535 / n * n
do {
cx16.r0 = math.rndw()
} until cx16.r0 < cx16.r1
return cx16.r0 % n
}
sub rndseed(uword seed1, uword seed2) { sub rndseed(uword seed1, uword seed2) {
; -- reset the pseudo RNG's seed values. Defaults are: $a55a, $7653. ; -- reset the pseudo RNG's seed values. Defaults are: $a55a, $7653.
%ir {{ %ir {{

View File

@@ -458,6 +458,12 @@ but perhaps the provided ones can be of service too.
``rndw ()`` ``rndw ()``
Returns next random word 0-65535 from the pseudo-RNG sequence. Returns next random word 0-65535 from the pseudo-RNG sequence.
``randrange (ubyte n) -> ubyte``
Returns random byte uniformly distributed from 0 to n-1 (compensates for divisibility bias)
``randrangew (uword n) -> uword``
Returns random word uniformly distributed from 0 to n-1 (compensates for divisibility bias)
``rndseed (uword seed1, uword seed2)`` ``rndseed (uword seed1, uword seed2)``
Sets a new seed for the pseudo-RNG sequence (both rnd and rndw). The seed consists of two words. Sets a new seed for the pseudo-RNG sequence (both rnd and rndw). The seed consists of two words.
Do not use zeros for the seed! Do not use zeros for the seed!

View File

@@ -1,25 +1,43 @@
%import textio %import textio
%import string %import math
%zeropage basicsafe %zeropage basicsafe
%option no_sysinit %option no_sysinit
main { main {
sub start() { sub start() {
screen.text_colors = [6,5,4,3,2,1] uword[7] rolls
for cx16.r0L in screen.text_colors { txt.print("wait...\n")
repeat 30 {
repeat 1000 {
unroll 10 rolls[math.rnd() % len(rolls)]++
}
}
for cx16.r0L in 0 to len(rolls)-1 {
txt.print_ub(cx16.r0L) txt.print_ub(cx16.r0L)
txt.spc() txt.spc()
txt.print_uw(rolls[cx16.r0L])
txt.nl()
}
txt.print("wait...\n")
rolls = [0,0,0,0,0,0,0]
repeat 30 {
repeat 1000 {
unroll 10 rolls[math.randrange(len(rolls))]++
}
}
for cx16.r0L in 0 to len(rolls)-1 {
txt.print_ub(cx16.r0L)
txt.spc()
txt.print_uw(rolls[cx16.r0L])
txt.nl()
} }
txt.nl()
} }
} }
screen {
ubyte[6] text_colors
}
/*main222 { /*main222 {
sub start() { sub start() {