I have a function getrand100() which returns a random number from 0-100
Write a code snippet to print numbers 1-200 in a random order with none of them repeating. You may call getrand100() any number of time to get random number from 1 to 100.
Please help how to do this?
Create an array of numbers from 0 to 200. Then, use the Fisher-Yates Shuffle to shuffle them.
Since your
getrand100function only gives numbers from 0 to 100, write something like this:That will give you random numbers from 0 to 200.
Actually, it’s a little more complicated than that because the range of random numbers changes. That is, at first you want to generate a random number from 0 to 200. The next time, from 0 to 199, then 198, 197, etc. So you need a way to scale your random numbers. Something like the following will get you reasonably close to uniform distribution:
This assumes you have a rounding function.
rangeis the top of your range. So the first call,rangeis 200, then 199, etc.That won’t give you perfect uniform distribution, since you’ll only be working with two significant digits, but it will give an acceptable result for a classroom assignment.