Re: Unique Random Numbers
How about just make the array first and then mixing the numbers by
using the random function?
For example...
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(0));
int arr[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int repeat = rand() % HOW_MANY_TIMES_DO_YOU_WANT;
int pos = 0;
for (int i = 0; i < repeat; ++i)
{
pos = rand() % 20;
if(pos == 19)
swap(0, 19); // Swapping the first and the last
else
swap(pos, pos+1); // Swapping with the next
}
return 0;
}
I just made the source code without thinking deeply and using IDE so
if there is a silly mistake then please forgive me =)
Cheers,
On Mar 26, 10:29 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Xoomer" <shah...@gmail.com> wrote in message
news:1174833087.549940.19520@d57g2000hsg.googlegroups.com...
Hi,
Can some post a code with simple Unique Random Numbers.
Random numbers stored in arr[20] but unique.
Unique, but sorted.
#include <iostream>
#include <string>
#include <set>
#include <ctime>
int main()
{
std::set<int> RandomNumbers;
std::srand( std::clock() );
while ( RandomNumbers.size() < 20 )
RandomNumbers.insert( RandomNumbers.end(), rand() );
for ( std::set<int>::iterator it = RandomNumbers.begin(); it !=
RandomNumbers.end(); ++it )
std::cout << *it << " ";
std::cout << std::endl;
std::string wait;
std::getline( std::cin, wait );
}