Hi, this should be a straightforward question, I am writing some code to randomly generate a number, and it works fine the only issue is I don't know how to limit the range of numbers it generates, and currently it prints between 0 and 250.
Hi, this should be a straightforward question, I am writing some code to randomly generate a number, and it works fine the only issue is I don't know how to limit the range of numbers it generates, and currently it prints between 0 and 250.
EDIT: My previous approach fell victim to "modulo bias" in generating a random number.
Per my colleague Einar Thorsrud:
"It would be better to use a bit mask to limit the generated number to 0-127, and then throw away any generated numbers that are outside of the desired range (1 to 100 in this case)."
Instead, I would encourage you to use the approach here in this toy example: https://github.com/hlord2000/zephyr_rng_test
Thanks,
Helmut Lord
EDIT: My previous approach fell victim to "modulo bias" in generating a random number.
Per my colleague Einar Thorsrud:
"It would be better to use a bit mask to limit the generated number to 0-127, and then throw away any generated numbers that are outside of the desired range (1 to 100 in this case)."
Instead, I would encourage you to use the approach here in this toy example: https://github.com/hlord2000/zephyr_rng_test
Thanks,
Helmut Lord
Thank you so much, this helps a lot!
Hello Jennifer, I would encourage you to check the edit I made to my initial post.
Thanks,
Helmut Lord