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

I'm trying to write a program to generate random number, within a range of predetermined numbers, while not having any duplicate numbers.
for instance:
I need 5 (non duplicate) numbers, between 1 and 10.

I tried using Rand, and then looping to regenerate number that were duplicated. But, that sat there running for about an hour, and never got all the numbers to be unique.

Any help on this would be greatly appreciated.

Thanks in Advance,
-Charles...

2007-11-30 00:20:25 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

Create a boolean array of 11 elements and initialise all the elements to false.
Now get a random number between 1 and 10
check the array [randomNumber] to see if you have that number (false means the number has not been used yet).
Now set array [randomNumber] to true.
If you have already had that number then get another random number.

You can either do something with the numbers when you first know that they have not been used or you can use the array to list the numbers in sequence.

2007-11-30 00:32:24 · answer #1 · answered by AnalProgrammer 7 · 0 0

I don't know what your code is, so I can't tell you what's wrong with it. When I wrote this code, I didn't run into the problem of it hanging. It takes... well, it seems "instant." Try this:

####### CODE #######
#include
using namespace std;

#define howMany 5
#define low 1
#define high 10

bool in_array(int num, int *array)
{
for (int i = 0; i < howMany; i++)
{
if (array[i] == num)
return true;
}
return false;
} // end in_array()

int main(int argc, char *argv)
{
srand(time(NULL));

int currentNum;
int nums[howMany];
for (int i = 0; i < howMany;)
{
currentNum = (rand() % (high - low + 1)) + low;
if (!in_array(currentNum, nums))
{
nums[i++] = currentNum;
cout << currentNum << "\n";
}
}

return 0;
} // end main()
####### END CODE #######

I wrote this so you can specify you high and low values (inclusive) and how many unique numbers you want from that range.

2007-11-30 22:53:20 · answer #2 · answered by Wiseguy 4 · 0 0

well I'd say make a class for random generators.

The class has "boxes" for each number via an array.

Now
array[i] contains i, but, when array[i] is chosen, you switch it with the last value in the array, then % by one less, so that part of the array isn't a choice, it'd be a memory hog for huge number sets though.

2007-11-30 08:36:15 · answer #3 · answered by lansingstudent09101 6 · 0 1

C++ isnt my area, but i hope this i found on the internet helps

Good luck!

http://www.tutorialized.com/view/tutorial/Random-number-within-a-range/5492

and put an if in to match each one of the results and if they match start again

i do this in visual basic, and it doesnt take an hour to get unique numbers

2007-11-30 08:33:46 · answer #4 · answered by PistolPete 2 · 0 0

I'm not using C++ these days.

Did you look at Linux kernel's random number generator? It is a popular method. I haven't studied it yet.

2007-11-30 08:54:07 · answer #5 · answered by Anonymous · 0 1

oh try this link, maybe a solution.


http://www.daniweb.com/forums/thread75415.html

thanks

2007-11-30 08:44:17 · answer #6 · answered by apple pie 1 · 0 0

fedest.com, questions and answers