0 Members and 1 Guest are viewing this topic.

;; assuming 'a' register already has random calculated numberld b, #42 ;; highest value + 1cp bjr c, _returnValuesub b_returnValue:ld l,aret

Maybe you can compare with highest value, and substract it if carry (for example)Code: [Select];; assuming 'a' register already has random calculated numberld b, #42 ;; highest value + 1cp bjr c, _returnValuesub b_returnValue:ld l,aret

Thanks for the answer, but I think that this method only works while the random number is lower than 84, becasue if it's higher than that, when you substract 42 (sub b), you get a number higher than 41... so, the result it's out of range, isn't it??

SpDizzy almost had it:Label the "Dec b" as "_loop", and put "jr , _loop" after the "sub b"Now the routine will loop until the value in a is less than 42.

;; assuming 'a' register already has random calculated numberld b, #42 ;; highest value + 1_loop:cp bjr c, _returnValuesub bjr _loop_returnValue:ld l,aret

If you already have a 8x8bit multiplication, just multiply the random number by 42, and keep the MSB of the result.

Oh my bad, just as simple as that! Time for me to go to bed...

Would this also work? Create a random 8 Bit number, get the upper 3 bits 0-7 and the lower 5 bits 0-31, add these values (0-38) and finally add the lowest 2 bits (0-3) of register R. That should give you a number between 0-41.

Real random numbers aren't evenly distributed anyway,

though I suspect the distribution won't necessarily be very even.

I have tested both @eto 's method and SpDizzy's ( as modified) and ego's is slightly more effective at being random.

can you share why? The only thing I see could be R, but at least if the random number generation is not at predictable times, R should be "random" enough to ensure the even distribution as long as the random number generator is good. I'd love to test this now, but I don't have access to my machine right now, as I'm on vacation.

Yes, largely because of R which has a habit of introducing predictability to RNGs.