Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random_shuffle 1

Status
Not open for further replies.

benp07

Technical User
Aug 31, 2001
87
0
0
US
i am trying to write a program to randomly shuffle numbers the program works but everytime i run the programs and get the same numbers shuffled in the same order

output 2 6 9 5 4 3 1 7 8 10

is there a way that i can make it so everytime i run the program it will be in a different order
example
2 3 8 5 9 10 7 1 6 4 (first run)
9 5 6 4 7 3 10 2 8 1 (second run) and so on

my program is at the end

Thanks for help a newbee
Ben



#include <iostream>

using std::cout;
using std::endl;

#include <algorithm>
#include <numeric>
#include <vector>

int main()
{
const int size = 10;
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

std::vector< int > v( a1, a1 + size );
std::eek:stream_iterator< int > output( cout, &quot; \n &quot; );

std::random_shuffle( v.begin(), v.end() );
std::copy( v.begin(), v.end(), output );

return 0;
}
 
That's normal behavior for random number generators. Using the same algorithm, you'll get the same sequence of numbers every time unless you specify a different starting value, or &quot;seed.&quot;

The rand function from the C library generates random numbers. You can call srand with a seed value (normally the current time) to get a different sequence every time.

Instead of rand_shuffle's default algorithm, you'll need to use a function that lets you seed it (such as rand). As far as I know, there's no standard way to seed random_shuffle's default.

It does, however, have an optional third parameter specifying a function or function object to call to get the random number. If I remember correctly, rand() does not fit this signature, so you could write a wrapper function object around it so it works. Then remember to call srand at some point before you use rand (probably in the function object constructor; use a static flag to make sure it gets called just once).

Alternately, has a whole set of random-number-generation libraries. I assume they probably have something you could feed to random_shuffle.
 
The srand and rand are very powerful, and are standard functions. instead of running to boost, seed the generator srand() with the current time in milliseconds and then pass rand() as your random function.
 
The srand and rand are very powerful, and are standard functions. instead of running to boost, seed the generator srand() with the current time in milliseconds and then pass rand() as your random function.
 
I'd suggest using rand first, too, then going to boost later if you need more specialization in your randomness.

The problem I pointed out above still stands, however: rand doesn't match the type that random_shuffle expects for a third argument. random_shuffle wants a function or function object that takes a positive integer N as an argument and returns a random integer between 0 and N. rand doesn't take an argument.

So you still need to write a wrapper around it. Here's my suggestion:

Code:
class Random
{
  public:

    Random()
    {
        if ( !seeded )
        {
            srand( time( 0 ) );
            seeded = true;
        }
    }

    int operator()( int n )
    {
        return rand() % n;
    }

  public:

    static bool seeded;
};

bool Random::seeded = false;


You could then call
Code:
random_shuffle( c.begin(), c.end(), Random() );
to use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top