Challenge: Random number generator based on an parameter from 0-100 that reprezents the chance to output a bolean 0 or 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Challenge: Random number generator based on an parameter from 0-100 that reprezents the chance to output a bolean 0 or 1

any language and i sugest making it a function or a method witf a parameter int chance and the return type of bool ex random(40) would have a 40% chance to output 1 and 60% chance to output 0 bonus chalenge: try to make it work wih float values as parameter

9th Nov 2017, 5:14 AM
Meemknight
Meemknight - avatar
4 Answers
+ 13
Here's my C# implementation! ✌ I decided to give it a try after having a little talk of randomness with Calviղ. Just increase the number of toss to observe the Law of Large Number in action. Enjoy~ ❤ https://code.sololearn.com/cXuX2eYWTVPs/?ref=app
9th Nov 2017, 2:58 PM
Zephyr Koo
Zephyr Koo - avatar
+ 3
Shouldn't be too hard (in js, anyway :P) https://code.sololearn.com/WODUzBdruUzk/?ref=app Returns true and false, if you really need either 1 or 0 you can convert it with Number(rand(50))
9th Nov 2017, 5:27 AM
Dorumin
Dorumin - avatar
+ 2
We would always get 50/50 for testing above 1 million Boolean numbers https://code.sololearn.com/WSCT2fF3mF49/?ref=app
9th Nov 2017, 6:10 AM
Calviղ
Calviղ - avatar
+ 2
Work-in-progress This demonstrates a 60/40 bias produced by using a modulus that does not evenly divide RAND_MAX, a problem for c++ . I'm working on a way to choose the split, for now I want to show the effect of the bias in this relevant context. (not showing 100 yet because of lag; will try to address that) Reference: https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator https://code.sololearn.com/WmOmcQ9HYcDR/?ref=app
12th Nov 2017, 1:35 AM
Kirk Schafer
Kirk Schafer - avatar