So I'm trying to find a way to basically generate 100 numbers from 0 to 99 in random order with no duplicates. This is my code, and as you can see it displays 100 numbers but....some are duplicates.

srand((unsigned)time(0));
for (int count = 0; count < 100; count++)
{
int y;

y = (rand()%100)+1;
cout<<y<<endl;


}

Recommended Answers

All 2 Replies

You could allocate an array of 1 to 100, then shuffle it.

Or, as you generate a new number, scan the current array content looking for duplicate, reject the number if found. But that becomes a time consuming operation, the more full the array gets. I'd go with the first option.

Just to add one quick thing: use a while loop instead so that you can exit when you have 100 numbers not just when you've gone through the cycle 100 times (I suppose you could rework your for loop to accomplish the same thing too but it might be more difficult to read).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.