Board index » cppbuilder » Random_shuffle

Random_shuffle


2008-05-28 09:16:26 PM
cppbuilder23
Hi
I've tried using the random shuffle() Template It seems to work fine
but it generates the same sequenses every time it is run.. Ive tried
various calling sequences
To change the sequences to no avail.
Could someone explain to me how to do this?
an example of calling sequence would be nice.
Ralph
 
 

Re:Random_shuffle

Ralph Gamble wrote:
Quote
Hi
I've tried using the random shuffle() Template It seems to work fine
but it generates the same sequenses every time it is run.. Ive tried
various calling sequences
To change the sequences to no avail.
Could someone explain to me how to do this?
an example of calling sequence would be nice.

Ralph
See bytes.com/forum/thread63524.html for a discussion of this issue.
HTH
Dennis Cote
 

Re:Random_shuffle

"Dennis Cote" < XXXX@XXXXX.COM >wrote in message
Quote
Ralph Gamble wrote:
>I've tried using the random shuffle() Template It seems to work fine
>but it generates the same sequenses every time it is run.. ...
>Could someone explain to me how to do this?

See bytes.com/forum/thread63524.html for a discussion of this
issue.
Yeesh, that was a lot of reading to find out that nobody in that thread
remembered you could pass your own random number generator to
random_shuffle.
Ralph you could do something like this:
=== BEGIN EXAMPLE ===
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
using namespace std;
int rng_rand (int m) {
return rand() % m;
}
int main () {
int numbers[5] = { 1, 2, 3, 4, 5 };
srand(time(NULL));
random_shuffle(numbers, numbers + 5, rng_rand);
copy(numbers, numbers + 5, ostream_iterator<int>(cout, "\n"));
}
=== END EXAMPLE ===
There, you tell random_shuffle that you want to use your own random number
generator, the function rng_rand. Then, you use rand() for your source of
randomness, and consequently you can reliably use srand() to seed the value.
In that example the value is seeded with time(NULL), which is the current
system time. This is a pretty typical seed value (although note that time()
only changes once per second so if your application will be starting more
frequently than that, use a different seed). Just call srand() once when
your application starts.
The disadvantage to the above example is rand() is not a very good RNG, but
it does get the job done. If you have a better source of randomness you can
substitute it there. Also, note that you can pass a reference to any object
with a () operator defined instead of a simple function pointer as the third
parameter to random_shuffle, if that is more appropriate.
See:
www.sgi.com/tech/stl/random_shuffle.html
www.sgi.com/tech/stl/RandomNumberGenerator.html
www.sgi.com/tech/stl/functors.html
Hope that helps,
Jason
 

{smallsort}

Re:Random_shuffle

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
int rng_rand (int m) {
return rand() % m;
}
On second thought, you'll want to do this instead:
int rng_rand (int m) {
return (int)((double)m * rand() / RAND_MAX) % m;
}
That way it will at least scale to m if m>RAND_MAX.
Jason
 

Re:Random_shuffle

Jason Cipriani wrote:
Quote

Yeesh, that was a lot of reading to find out that nobody in that thread
remembered you could pass your own random number generator to
random_shuffle.

The OP contacted me off list for additional follow up.
I thought the important points were all hit upon: The behavior is
currently implementation defined. The new standard will change that so
that random_shuffle can use rand() by default. The Dinkumware libs
already use rand(). All the OP needs to do is call srand() once to seed
the generator so it doesn't always start with the same sequence.
There is no need to use your own random number generator. Simply ensure
rand() is seeded with different values if you want different random
sequences.
Dennis Cote
 

Re:Random_shuffle

"Dennis Cote" < XXXX@XXXXX.COM >wrote in message
Quote
I thought the important points were all hit upon: The behavior is
currently implementation defined. The new standard will change that so
that random_shuffle can use rand() by default. The Dinkumware libs already
use rand(). All the OP needs to do is call srand() once to seed the
generator so it doesn't always start with the same sequence.

There is no need to use your own random number generator. Simply ensure
rand() is seeded with different values if you want different random
sequences.
Sure, but it's still important to point out that C++0x is not out yet, it
may be some more time after it's actually released before runtime libraries
conform, and even more time before developers are actually using it (e.g.
look at the BDS5 and 6 posts that are still being made here). So while in
the future that's fine, right now it's not guaranteed all around.
And, while it *is* guaranteed with Dinkumware's implementation (meaning that
for the OP it should probably work just fine for his particular situation),
if he happens to be, say, writing a section of code that is designed to be
use outside of the context BCB (which was unspecified), he may be surprised
to find that srand() works when he compiles it in BCB but not on another
platform.
Currently, as the discussion covers, the standard does not require rand(),
and does not require srand() to affect random_shuffle. Therefore, if one is
going to have a detailed discussion about standards, such as precise
implementation details in C++0x, then one should be consistent and also
place the same amount of importance on current standards. On the other hand
it does make sense to do whatever works with BCB; but the OP didn't specify
enough information (at least in public on this newsgroup) to indicate that
relying on BCB's specific behavior was safe.
So, no, it's not necessary to provide your own random number generator if
you are using BCB + Dinkumware or are going to wait around for C++0x, but
it's definitely important to point out the consequences of making those
assumptions.
Jason
 

Re:Random_shuffle

Thank you both
I was using BCB 5
it does not use Dinkumware I dont think i think it uses Rougue wave
and i was unable to use Srand(}
I see how to do that now.
sorry for late response I was away.
thanks again
Ralph
"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
"Dennis Cote" < XXXX@XXXXX.COM >wrote in message
news:484574f1$ XXXX@XXXXX.COM ...
>I thought the important points were all hit upon: The behavior is
>currently implementation defined. The new standard will change that
>so that random_shuffle can use rand() by default. The Dinkumware
>libs already use rand(). All the OP needs to do is call srand()
>once to seed the generator so it doesn't always start with the same
>sequence.
>
>There is no need to use your own random number generator. Simply
>ensure rand() is seeded with different values if you want different
>random sequences.

Sure, but it's still important to point out that C++0x is not out
yet, it may be some more time after it's actually released before
runtime libraries conform, and even more time before developers are
actually using it (e.g. look at the BDS5 and 6 posts that are still
being made here). So while in the future that's fine, right now it's
not guaranteed all around.

And, while it *is* guaranteed with Dinkumware's implementation
(meaning that for the OP it should probably work just fine for his
particular situation), if he happens to be, say, writing a section
of code that is designed to be use outside of the context BCB (which
was unspecified), he may be surprised to find that srand() works
when he compiles it in BCB but not on another platform.

Currently, as the discussion covers, the standard does not require
rand(), and does not require srand() to affect random_shuffle.
Therefore, if one is going to have a detailed discussion about
standards, such as precise implementation details in C++0x, then one
should be consistent and also place the same amount of importance on
current standards. On the other hand it does make sense to do
whatever works with BCB; but the OP didn't specify enough
information (at least in public on this newsgroup) to indicate that
relying on BCB's specific behavior was safe.

So, no, it's not necessary to provide your own random number
generator if you are using BCB + Dinkumware or are going to wait
around for C++0x, but it's definitely important to point out the
consequences of making those assumptions.

Jason

 

Re:Random_shuffle

Please direct your browser at www.teamb.com/newsgroups and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!