Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > rand() chooses the same number after several trials

Reply
Thread Tools

rand() chooses the same number after several trials

 
 
kkirtac
Guest
Posts: n/a
 
      08-29-2007
Hello, i m using the standard rand() function to generate several
random numbers. Even if i seed the generator before the loop
"srand( (unsigned)time( NULL ) );" , it usually selects a previously
selected number in the process, after 6-7 iterations, i want the
sequence to be unique..should i consider some further modifications in
the code to achieve my goal maybe ? here is the piece of code..

vector<int> rands ;
int rnd ; //random number
srand( (unsigned)time( NULL ) );
int range_max = 165, range_min = 0 ;
for( int i = 0; i < 15 ; i++ )
{
rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
range_min;
rands.push_back(rnd);
}

it usually chooses a previously selected random number after 7-8
iterations, not all the time but usually..

Regards,

 
Reply With Quote
 
 
 
 
zacariaz@gmail.com
Guest
Posts: n/a
 
      08-29-2007
On 29 Aug., 11:50, kkirtac <kadir.kir...@gmail.com> wrote:
> Hello, i m using the standard rand() function to generate several
> random numbers. Even if i seed the generator before the loop
> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
> selected number in the process, after 6-7 iterations, i want the
> sequence to be unique..should i consider some further modifications in
> the code to achieve my goal maybe ? here is the piece of code..
>
> vector<int> rands ;
> int rnd ; //random number
> srand( (unsigned)time( NULL ) );
> int range_max = 165, range_min = 0 ;
> for( int i = 0; i < 15 ; i++ )
> {
> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
> range_min;
> rands.push_back(rnd);
>
> }
>
> it usually chooses a previously selected random number after 7-8
> iterations, not all the time but usually..
>
> Regards,


nomatter what you do it will allways be psudo random. By using complex
math and stuff you can it seem like it isnt, but it is. there are
other options though. Ive heard of people experimenting with geneating
random numbers by the help of the soundcard static and simular, but
really i dont think that is a god option. Allso there are devices that
you can plug into your computer which generates truly random number.
My favorite so far is: http://random.irb.hr/
however it relys on you having got a internet conenction.

 
Reply With Quote
 
 
 
 
Philip Potter
Guest
Posts: n/a
 
      08-29-2007
Abdo Haji-Ali wrote:
> "kkirtac" <> wrote in message
>> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
>> range_min;

> Why not use:
> rnd = range_min + rand() % rang_max;
> instead? I think it's much clearer and you gain some performance by omitting
> the extra multiplication, don't know why some use the style you're using
> though.


Because the lower bits of a PRNG are often much less random than the higher
bits. See the comp.lang.c FAQ, question 13.16:
http://c-faq.com/lib/randrange.html

The OP's code is better quality under such conditions.

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      08-29-2007
kkirtac wrote:
> Hello, i m using the standard rand() function to generate several
> random numbers. Even if i seed the generator before the loop
> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
> selected number in the process, after 6-7 iterations, i want the
> sequence to be unique..should i consider some further modifications in
> the code to achieve my goal maybe ? here is the piece of code..
>
> vector<int> rands ;
> int rnd ; //random number
> srand( (unsigned)time( NULL ) );
> int range_max = 165, range_min = 0 ;
> for( int i = 0; i < 15 ; i++ )
> {
> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
> range_min;
> rands.push_back(rnd);
> }
>
> it usually chooses a previously selected random number after 7-8
> iterations, not all the time but usually..


Even with a perfect random number generator, this may well happen, due to a
thing called the "birthday paradox":
http://en.wikipedia.org/wiki/Birthday_paradox

This states (in terms applicable here) that given 23 numbers randomly chosen
from the range [1,365] it is more likely than not that they will be nonunique.
IOW, given 23 random people, it is more likely than not that two share a birthday.

In your case, given a perfect rand() the probability that 8 numbers will be
unique in the range [1,165] is 80.1%. That's more likely than not, but about 1
time in 5 you'll have collisions.

You should modify your code to prevent collisions entirely if you wish to avoid
them entirely. This is comp.lang.c FAQ question 13.19; see http://c-faq.com/ for
details.

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
Abdo Haji-Ali
Guest
Posts: n/a
 
      08-29-2007
"kkirtac" <> wrote in message
news: oups.com...
> Hello, i m using the standard rand() function to generate several
> random numbers. Even if i seed the generator before the loop
> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
> selected number in the process, after 6-7 iterations, i want the
> sequence to be unique..should i consider some further modifications in
> the code to achieve my goal maybe ?

Yes... rand() generate pseudorandom numbers, but it doesn't guarantee those
numbers to be unique.
Consider when generating a random number between 0 and 1 (i.e. only 0 or 1),
it wouldn't be so random then if I got a 0 then 1 then 0 then 1
(alternatively)
You need to check that you didn't push the generated number in 'rands'
before; if that's the case you should generate another random number and
pray it will be different
Or you could search the internet for a better "shuffling" algorithm.

> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
> range_min;

Why not use:
rnd = range_min + rand() % rang_max;
instead? I think it's much clearer and you gain some performance by omitting
the extra multiplication, don't know why some use the style you're using
though.

Hope that helps,
--
Abdo Haji-Ali
Programmer
In|Framez


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      08-29-2007
kkirtac <> wrote:

> Hello, i m using the standard rand() function to generate several
> random numbers. Even if i seed the generator before the loop
> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
> selected number in the process, after 6-7 iterations, i want the
> sequence to be unique..should i consider some further modifications in
> the code to achieve my goal maybe ? here is the piece of code..
>
> vector<int> rands ;
> int rnd ; //random number
> srand( (unsigned)time( NULL ) );
> int range_max = 165, range_min = 0 ;
> for( int i = 0; i < 15 ; i++ )
> {
> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
> range_min;


Why the (RAND_MAX + 1)? On my system that is equal to INT_MIN. Why
(range_max - 1 - range_min)? That doesn't seem right either.

> rands.push_back(rnd);
> }


You should do your multiplies first, otherwise you loose precession. Try
this instead:

rnd = (double)rand() * (range_max - range_min) / RAND_MAX + range_min;

> it usually chooses a previously selected random number after 7-8
> iterations, not all the time but usually..

 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      08-29-2007
Daniel T. wrote:
> kkirtac <> wrote:
>
>> Hello, i m using the standard rand() function to generate several
>> random numbers. Even if i seed the generator before the loop
>> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
>> selected number in the process, after 6-7 iterations, i want the
>> sequence to be unique..should i consider some further modifications in
>> the code to achieve my goal maybe ? here is the piece of code..
>>
>> vector<int> rands ;
>> int rnd ; //random number
>> srand( (unsigned)time( NULL ) );
>> int range_max = 165, range_min = 0 ;
>> for( int i = 0; i < 15 ; i++ )
>> {
>> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
>> range_min;

>
> Why the (RAND_MAX + 1)? On my system that is equal to INT_MIN. Why


Ooh, nice catch, I didn't spot that.

> (range_max - 1 - range_min)? That doesn't seem right either.


I think the OP maybe meant (range_max + 1 - range_min), because this is the
number of values in the range [range_min,range_max] including both endpoints.

>> rands.push_back(rnd);
>> }

>
> You should do your multiplies first, otherwise you loose precession. Try
> this instead:
>
> rnd = (double)rand() * (range_max - range_min) / RAND_MAX + range_min;


For integers, divides lose precision while multiplies retain perfect accuracy.
For floats, they both lose about the same precision and order doesn't matter. If
you're using floating point the right line is probably:

rnd = (double)rand() / ((double)RAND_MAX + 1) * (range_max - range_min + 1) +
range_min;

But someone will probably see a bug in my code too, even though I'm just copying
from the clc FAQ.

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      08-29-2007
On 2007-08-29 07:18:06 -0400, "Abdo Haji-Ali"
<_use_com_instead> said:

> "kkirtac" <> wrote in message
> news: oups.com...
>> Hello, i m using the standard rand() function to generate several
>> random numbers. Even if i seed the generator before the loop
>> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
>> selected number in the process, after 6-7 iterations, i want the
>> sequence to be unique..should i consider some further modifications in
>> the code to achieve my goal maybe ?

> Yes... rand() generate pseudorandom numbers, but it doesn't guarantee those
> numbers to be unique.


And if the numbers were guaranteed to be unique, they would not be random.

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

 
Reply With Quote
 
Abdo Haji-Ali
Guest
Posts: n/a
 
      08-29-2007
"Philip Potter" <> wrote in message
news:fb3jd2$2qm$...
> Abdo Haji-Ali wrote:
>> "kkirtac" <> wrote in message
>>> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
>>> range_min;

>> Why not use:
>> rnd = range_min + rand() % rang_max;
>> instead? I think it's much clearer and you gain some performance by
>> omitting the extra multiplication, don't know why some use the style
>> you're using though.

>
> Because the lower bits of a PRNG are often much less random than the
> higher bits. See the comp.lang.c FAQ, question 13.16:
> http://c-faq.com/lib/randrange.html
>
> The OP's code is better quality under such conditions.

The more you know
Thanks a lot for the tip.

--
Abdo Haji-Ali
Programmer
In|Framez


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      08-29-2007
On 2007-08-29 05:50:39 -0400, kkirtac <> said:

> Hello, i m using the standard rand() function to generate several
> random numbers. Even if i seed the generator before the loop
> "srand( (unsigned)time( NULL ) );" , it usually selects a previously
> selected number in the process, after 6-7 iterations, i want the
> sequence to be unique..should i consider some further modifications in
> the code to achieve my goal maybe ? here is the piece of code..
>


Yes, that's how random numbers work. If you never got a duplicate value
until all the values in the range had been returned, the values would
not be random. (That's why casino operators get so upset with card
counters) To generate a sequence of unique values, look at the
algorithm 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
activex troubles and trials Mr Seth T ASP .Net 0 06-26-2007 06:36 PM
ASP.net trials on XP Professional chooser ASP .Net 0 06-28-2006 09:34 PM
DVD trials Martin DVD Video 1 03-17-2006 02:19 PM
So called online free trials news.xtra.co.nz NZ Computing 0 12-15-2005 12:40 AM
120 day trials? zorz22 MCSE 3 02-28-2004 10:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57