Need help with random number generator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need help with random number generator

Write a program that tests the validity of the rand() function. Generate 10,000 random numbers. For each random number, determine if it is even or odd, counting the total number of even and odd numbers. Compute the ratio of even to odd numbers. Print to the screen the number of even numbers generated, the number of odd numbers generated and the ratio of even to odd numbers. What should the ratio be if rand() is functioning properly? How does your result compare? I dont understand how to get the odd and even numbers

2nd Sep 2018, 2:48 PM
Adriel Murray
4 Answers
+ 2
Adriel Murray I made an example for you. https://code.sololearn.com/cZItR640xB50/?ref=app If you want more accurate result, just increase the trial times.
2nd Sep 2018, 3:17 PM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 2
The RNG is supposed to generate unbiased and uniform-distributed numbers. That is, if you repeat generating 10000 numbers in, for example 10 times, the average ratio of odd nums to even nums should be close to 1. Use modulo operator to check if a number is even or odd.
2nd Sep 2018, 2:56 PM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
2nd Sep 2018, 3:03 PM
Anna
Anna - avatar
+ 1
I wrote this real fast in Python, hopefully this helps https://code.sololearn.com/cj7im1GWDXnE/#py
2nd Sep 2018, 10:19 PM
Steven M
Steven M - avatar