English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I had written a program that outputs 5 randomized numbers from 1-10. The thing is, I like them to be unique. No numbers would be repeated. Any idea how?

2006-08-24 09:20:01 · 2 answers · asked by Ashley C 2 in Computers & Internet Programming & Design

2 answers

put the generated number in an array.
If the new generated number is in the array generate it again.
Use random seed of the time in your program.

2006-08-24 09:41:11 · answer #1 · answered by iyiogrenci 6 · 0 0

You need to seed your randomizer before you use it, otherwise you will always end up with the same numbers. You also need to make sure you seed it differently each time, otherwise, you will again, end up with the same result each time if you seed with a fixed number like 10.

The recommended practice is to use the srand (seed randomizer) method before using rand using some variation of the time() method as the parameter (i.e. srand(time(0));)

http://www.fredosaurus.com/notes-cpp/misc/random.html

2006-08-25 02:10:38 · answer #2 · answered by magrowan 1 · 0 0

fedest.com, questions and answers