473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unique Random Numbers

Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.

Mar 25 '07 #1
17 13469
On 2007-03-25 16:31, Xoomer wrote:
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[i] = rand();

return 0;
}

--
Erik Wikström
Mar 25 '07 #2
On Mar 25, 3:53 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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[i] = rand();

return 0;

}

--
Erik Wikström
Thanks. But the number may repeat the same.. its not Unique.

Mar 25 '07 #3
On Mar 25, 3:53 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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[i] = rand();

return 0;

}

--
Erik Wikström
Thanks. But the random number may repeat the same.. its not Unique.

Mar 25 '07 #4
Xoomer wrote:
Hi,

Can some post a code with simple Unique Random Numbers.

Random numbers stored in arr[20] but unique.
Your answer may be found at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Mar 25 '07 #5
"Xoomer" writes:
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.
Mar 25 '07 #6
Xoomer wrote:
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.
Mar 25 '07 #7
On 25 Mar 2007 07:59:37 -0700, "Xoomer" <sh*****@gmail.comwrote:
>On Mar 25, 3:53 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>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[i] = rand();

return 0;

}

--
Erik Wikström

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

Mar 25 '07 #8
rossum wrote:
Highly unlikely. The repeat length of any halfway decent RNG is a lot
longer than 20. Knuth Chapter Three refers.
That's repetition of the sequence, not of individual values.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 25 '07 #9
"Xoomer" <sh*****@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.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<intRandomNumbers;
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 );

}
Mar 26 '07 #10
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.comwrote:
"Xoomer" <shah...@gmail.comwrote in message

news:11*********************@d57g2000hsg.googlegro ups.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<intRandomNumbers;
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 );

}

Mar 26 '07 #11
Alexander D. B. Kim wrote:
How about just make the array first and then mixing the numbers by
using the random function?
Use std::random_shuffle.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 26 '07 #12
On Sun, 25 Mar 2007 16:14:23 -0400, Pete Becker
<pe**@versatilecoding.comwrote:
>rossum wrote:
>Highly unlikely. The repeat length of any halfway decent RNG is a lot
longer than 20. Knuth Chapter Three refers.

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

Mar 26 '07 #13
Thanks =)

On Mar 26, 9:47 pm, Pete Becker <p...@versatilecoding.comwrote:
Alexander D. B. Kim wrote:
How about just make the array first and then mixing the numbers by
using the random function?

Use std::random_shuffle.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)

Mar 26 '07 #14
On Mar 25, 7:31 am, "Xoomer" <shah...@gmail.comwrote:
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.

Mar 26 '07 #15
rossum wrote:
On Sun, 25 Mar 2007 16:14:23 -0400, Pete Becker
<pe**@versatilecoding.comwrote:
>>rossum wrote:
>>Highly unlikely. The repeat length of any halfway decent RNG is a lot
longer than 20. Knuth Chapter Three refers.

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.
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
Mar 26 '07 #16
Xoomer wrote:
On Mar 25, 3:53 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>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[i] = rand();

return 0;

}

--
Erik Wikström

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[i] == -1) //assign counter only if flag is found
{
arr[rand()%20] = i;
}
}
return 0;

}

Hope this helps...

regards,
Seemanta
Mar 27 '07 #17
On Mar 26, 9:53 pm, Seemanta Dutta <seema...@motorola.comwrote:
Xoomer wrote:
On Mar 25, 3:53 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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[i] = rand();
return 0;
}
--
Erik Wikström
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[i] == -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

Apr 11 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lallous | last post by:
Hello, This code works fine when 'size' is less than 32768 however when size is bigger this function never returns. Can't find out why?! If I break into the code I can see that 'i' is 32768.......
15
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI...
3
by: ronnchpra | last post by:
Hello, I need help generating 50 unique numbers using rand() BETWEEN 1 and 100 and assign them to a array. I have worked out the number randomization and assignment, but I cant figure out how to...
1
by: rlm51 | last post by:
Hey everyone ... happy Friday ... I need to generate a string of 11 random numbers, (which in itself is not a problem) however, each needs to be unique in value. Building a tree of logical...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.