|
Are you posting homework assignments? Why do you need different random numbers from 1 to 18?
There are 2 ways:
1/ keep generating random numbers, reject the ones you already have (use a hash to store them) until you have enough different ones
-> This creates a lot of extra work, and if you want more random numbers than available in your range, you'll get an infinite loop. Worst-case execution time is unpredictable (averages can be calculated with use of statistics)
2/ Generate an array of "accepted values" and pick a random element from the array, then remove it from the array.
-> Nice & fast if you got a small range, predictable worst-case scenario
If you want me to write out the script, take a minute to explain why you need it. If it's for homework, we won't solve it for you. We'll help you think and we'll look at your scripts and find mistakes, but won't write the whole thing for you.
|