Unique Random Numbers

X

Xoomer

Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.

From the top of my head:

#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
int arr[20];

for (int i = 0; i < 20; ++i)
arr = rand();

return 0;
}
 
O

osmium

Xoomer said:
Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.

The magic word is shuffle. See what you can find on the net.
 
J

Jacek Dziedzic

Xoomer said:
Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.

Well, if the array is only 20 elements long, why not
generate a random number for every entry i, and then
compare it with all previous numbers arr[0..i-1], if
it compares, throw it away and generate a new one?

Not very efficient, but for 20 elements it would be fine.

HTH,
- J.
 
R

rossum

Can some post a code with simpleUniqueRandomNumbers.
Randomnumbersstored in arr[20] butunique.

From the top of my head:

#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
int arr[20];

for (int i = 0; i < 20; ++i)
arr = rand();

return 0;

}


Thanks. But the number may repeat the same.. its not Unique.

Highly unlikely. The repeat length of any halfway decent RNG is a lot
longer than 20. Knuth Chapter Three refers.

rossum
 
J

Jim Langston

Xoomer said:
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 );

}
 
A

Alexander D. B. Kim

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,

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 );

}
 
S

Seemanta Dutta

Xoomer said:
Hi,
Can some post a code with simpleUniqueRandomNumbers.
Randomnumbersstored in arr[20] butunique.
From the top of my head:

#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
int arr[20];

for (int i = 0; i < 20; ++i)
arr = rand();

return 0;

}


Thanks. But the random number may repeat the same.. its not Unique.

Yes, this might generate repeated random numbers.
You can try generating the index randomly and assign
monotonically increasing count value to that array element,
after checking the presence of a flag.

Here's how:

int main()
{
srand(time(0));
int arr[20] = {-1}; //init the array with a flag

for (int i = 0; i < 20; ++i)
{
if(arr == -1) //assign counter only if flag is found
{
arr[rand()%20] = i;
}
}
return 0;

}

Hope this helps...

regards,
Seemanta
 
R

rossum

That's repetition of the sequence, not of individual values.
Correct, but with a reasonable linear congruential generator
individual values do not repeat until the whole sequence repeats. If
the OP limits responses to a given range then repeats do become
possible.

rossum
 
R

rmpowell77

Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.

Jon Bentley's book Programming Pearls Second Edition has this exact
question and discussion. Check chapter 1 problem 4.
 
K

Kai-Uwe Bux

rossum said:
Correct, but with a reasonable linear congruential generator
individual values do not repeat until the whole sequence repeats.

That's one of their weaknesses: the Birthday paradox shows that repetitions
_should_ happen much earlier.
If
the OP limits responses to a given range then repeats do become
possible.

They also become possible if the OP uses a different RNG.


Best

Kai-Uwe Bux
 
A

Alexander D. B. Kim

Xoomer said:
On 2007-03-25 16:31, Xoomer wrote:
Hi,
Can some post a code with simpleUniqueRandomNumbers.
Randomnumbersstored in arr[20] butunique.
From the top of my head:
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(0));
int arr[20];
for (int i = 0; i < 20; ++i)
arr = rand();
return 0;
}

Thanks. But the random number may repeat the same.. its not Unique.

Yes, this might generate repeated random numbers.
You can try generating the index randomly and assign
monotonically increasing count value to that array element,
after checking the presence of a flag.

Here's how:

int main()
{
srand(time(0));
int arr[20] = {-1}; //init the array with a flag

for (int i = 0; i < 20; ++i)
{
if(arr == -1) //assign counter only if flag is found
{
arr[rand()%20] = i;
}
}
return 0;

}

Hope this helps...

regards,
Seemanta


what if "rand()%20" keeps generating the same number (what a
coincidence!!!) =P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top