# CPCWiki forum

## General Category => Programming => Topic started by: johnlobo on 12:31, 14 August 21

Title: random number between 0 and 41
Post by: johnlobo on 12:31, 14 August 21
Hi,
I'm generating in assembler a pseudo random number of one byte (0 to 255) , but a need to limit the result to a number between 0 to 41.

Is there an easy way to do this?? I'm trying to avoid to include a routine to calculate the modulus just for this.

Regards.
Title: Re: random number between 0 and 41
Post by: SpDizzy on 13:14, 14 August 21
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`
Title: Re: random number between 0 and 41
Post by: TotO on 13:32, 14 August 21
Modulo?
Title: Re: random number between 0 and 41
Post by: johnlobo on 14:13, 14 August 21
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??

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`
Title: Re: random number between 0 and 41
Post by: Bread80 on 14:31, 14 August 21
If you just do a modulo then the result won't be truly random. Since 255 isn't an even multiple of 42 some output values will have a slightly higher probability than others. Ie. 42*6 = 252. So the values 252..255 will modulo to a value of 0..3 (if you repeatedly modulo until in range). Thus 0-3 will have a higher probability of being generated than 4-41.

You can try dividing the initial RNG output, but with an 8-bit RNG the resolution won't be great - you may want to consider using a 16-bit one.

The other option would be to generate another number if the output isn't in range. This obviously carries a time penalty.

The compromise might be to re-run for number >= 252 and modulo any other value.

(All of which depends on your need for randomness and your need for speed).
Title: Re: random number between 0 and 41
Post by: Cwiiis on 14:51, 14 August 21
If you're not too worried about the weighting being uneven, you could discard the top 2 bits (so you end up with a number between 0-63) and take away 42 if it's greater than 42... I suppose you could also use the discarded bits to choose 4 different strata of number to subtract between 21-42 and reduce the impact of the otherwise obvious weighting.
Title: Re: random number between 0 and 41
Post by: SpDizzy on 14:55, 14 August 21
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??
Of course, sorry, that's my fault.

Out of modulus, I think as @Bread80 (https://www.cpcwiki.eu/forum/index.php?action=profile;u=4222) has said, generate another random number if the output is out of range may be a possibility.

Even better, discard top 2 bits as @Cwiiis (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3482) suggested looks fine to me.
Title: Re: random number between 0 and 41
Post by: gerald on 16:36, 14 August 21
If you already have a 8x8bit multiplication, just multiply the random number by 42, and keep the MSB of the result.
Title: Re: random number between 0 and 41
Post by: Animalgril987 on 22:06, 14 August 21
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.
Title: Re: random number between 0 and 41
Post by: SpDizzy on 22:49, 14 August 21
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.
Oh my bad, just as simple as that! Time for me to go to bed...

Code: [Select]
`;; assuming 'a' register already has random calculated numberld b, #42  ;; highest value + 1_loop:cp bjr c, _returnValuesub bjr _loop_returnValue:ld l,aret`
Title: Re: random number between 0 and 41
Post by: johnlobo on 11:18, 15 August 21
Yes, now it works.

If I don't find anything more accurate, I'll go with this.
Thank you.

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.
Title: Re: random number between 0 and 41
Post by: johnlobo on 11:19, 15 August 21
Yes I have implemented 8x8 multiplication... I will try this too.
Thank you.

If you already have a 8x8bit multiplication, just multiply the random number by 42, and keep the MSB of the result.
Title: Re: random number between 0 and 41
Post by: Animalgril987 on 15:53, 15 August 21
@SpDizzy (https://www.cpcwiki.eu/forum/index.php?action=profile;u=2740)
Oh my bad, just as simple as that! Time for me to go to bed...
I have those kind of days too :D
Title: Re: random number between 0 and 41
Post by: eto on 16:36, 16 August 21
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.
Title: Re: random number between 0 and 41
Post by: Animalgril987 on 20:37, 16 August 21
@eto (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3625): That's an interesting and novel approach :D
It should work ok, and if the OP isn't using R for his random number, it will add an "extra randomness" to the bottom 2 bits.
Title: Re: random number between 0 and 41
Post by: andycadley on 21:11, 16 August 21
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.
It will give you a number in the right range, though I suspect the distribution won't necessarily be very even.
Title: Re: random number between 0 and 41
Post by: Animalgril987 on 21:34, 16 August 21
@andycadley (https://www.cpcwiki.eu/forum/index.php?action=profile;u=327)  Real random numbers aren't evenly distributed anyway, just take a look at a lottery draw: there are often "clumps" of numbers at one end or in the middle.  :D
Title: Re: random number between 0 and 41
Post by: andycadley on 01:02, 17 August 21
Indeed. But a property of a "good" random number routine is usually that it gives a reasonably even distribution over a long enough sample set, "clumps" can lead to results seeming too predictable or pattern like.

Obviously it depends what you're doing. Sometimes good enough is all it needs.
Title: Re: random number between 0 and 41
Post by: eto on 15:29, 17 August 21
Real random numbers aren't evenly distributed anyway,

then they are not good random numbers. At least when the sample size is big enough, the random numbers should be (almost) evenly distributed.
Title: Re: random number between 0 and 41
Post by: eto on 15:34, 17 August 21
though I suspect the distribution won't necessarily be very even.

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.
Title: Re: random number between 0 and 41
Post by: Animalgril987 on 19:33, 17 August 21
I have tested both @eto (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3625) 's method and SpDizzy's ( as modified) and ego's is slightly more effective at being random.
Title: Re: random number between 0 and 41
Post by: SpDizzy on 20:26, 17 August 21
I have tested both @eto (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3625) 's method and SpDizzy's ( as modified) and ego's is slightly more effective at being random.
In cases where speed is highly compromised, @eto (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3625)'s method always take same amount of microseconds and is a bit faster,  whereas mine could be slightly slower for bigger numbers, since you are repeating the loop until random number is below 42. On respect to size, I guess eto's takes 3 - 4 bytes more.
Title: Re: random number between 0 and 41
Post by: andycadley on 22:30, 17 August 21
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. As you say, it may not make a difference though and you'd have to try it out to see for sure.
Title: Re: random number between 0 and 41
Post by: eto on 11:34, 18 August 21
Yes, largely because of R which has a habit of introducing predictability to RNGs.

All pseudo random generators generate predictable sequences (unless you are using a different seed every time but then also R is no longer an issue I guess).
Title: Re: random number between 0 and 41
Post by: Targhan on 11:39, 18 August 21