srand() question

Discussion in 'C++' started by Graeme, Jul 29, 2003.

  1. Graeme

    Graeme Guest

    I am writing a simple windows matching game with cards and pictures
    (ie "concentration"). In order to write something like this, I need
    to be able to give each "card" a random position in the assortment,
    and I need the assortment to be different each time the program is
    run. (I think) This means I need to be able to generate as many
    random numbers at once as the number of "cards" in the assortment when
    the program is first loaded. I have tried seeding the rand() function
    with GetTickCount() as well as time():

    srand(GetTickCount());

    but the loop that generates the numbers is executed too fast, and I
    end up getting 36 identical numbers. Is there a way to generate many
    random numbers at once, and have them be different each time I run the
    program? Possibly using recursion in the rand or srand functions?
    Any help is greatly appreciated!

    Thanks
    Graeme
     
    Graeme, Jul 29, 2003
    #1
    1. Advertising

  2. Graeme

    David White Guest

    Graeme <> wrote in message
    news:...
    > I am writing a simple windows matching game with cards and pictures
    > (ie "concentration"). In order to write something like this, I need
    > to be able to give each "card" a random position in the assortment,
    > and I need the assortment to be different each time the program is
    > run. (I think) This means I need to be able to generate as many
    > random numbers at once as the number of "cards" in the assortment when
    > the program is first loaded.


    It sounds like the same as dealing cards at random from a deck for a card
    game.

    > I have tried seeding the rand() function
    > with GetTickCount() as well as time():
    >
    > srand(GetTickCount());
    >
    > but the loop that generates the numbers is executed too fast, and I
    > end up getting 36 identical numbers.


    This doesn't make sense to me. Each time you run the program the tick count
    will be different, so if the rand() implementation is any good you should
    get a different sequence of random numbers each time. The execution time of
    the loop should be irrelevant, since it's only the single call to srand()
    before the loop that determines the sequence returned by rand() inside the
    loop. I would not expect the value returned by rand() to be affected by how
    long ago it was last called.

    > Is there a way to generate many
    > random numbers at once, and have them be different each time I run the
    > program? Possibly using recursion in the rand or srand functions?


    I'm sure you don't need anything as exotic as a recursive rand function,
    whatever that would do. I think it's a much simpler problem than that.

    DW
     
    David White, Jul 29, 2003
    #2
    1. Advertising

  3. Graeme

    Yamin Guest

    "Graeme" <> wrote in message
    news:...
    > I am writing a simple windows matching game with cards and pictures
    > (ie "concentration"). In order to write something like this, I need
    > to be able to give each "card" a random position in the assortment,
    > and I need the assortment to be different each time the program is
    > run. (I think) This means I need to be able to generate as many
    > random numbers at once as the number of "cards" in the assortment when
    > the program is first loaded. I have tried seeding the rand() function
    > with GetTickCount() as well as time():
    >

    [snip]

    Hi Graeme,

    might I suggest a simpler alternative.
    Just keep getting random numbers until you get the number of random numbers
    you need. Performance should be a non-issue in such an app. Ignore
    repeated random numbers.

    If you want want to shield from an infinite loop just incase rand() really
    cannot get all the numbers:
    prefill all numbers with numbers you generate manually, then rand() the
    remaining. Say you only need to have 32 random numbers:
    **********************************
    //prefill the array with you initial values
    std::vector<int> randNums;
    randNums.resize(32);

    srand(time() );
    for( int i =0; i < 1024; i++)
    {
    int x = rand();
    //do your scaling on x...i.e. x = x % 32

    if ( ! in_randNums(x) ) //some function which checks if the number is
    already in the array
    {
    randNums[ i % 32] = x;
    }

    }
    **********************************


    Yeah, its not the mose efficient, but it works.

    Yamin
     
    Yamin, Jul 29, 2003
    #3
  4. Graeme

    Bruce Guest

    In comp.lang.c++
    "Yamin" <> wrote:

    >might I suggest a simpler alternative.
    >Just keep getting random numbers until you get the number of random numbers
    >you need. Performance should be a non-issue in such an app. Ignore
    >repeated random numbers.


    See an algorithm in Programming Pearls by Jon Bentely about generating a
    random set. It is very clever and will run in O(n) speed.
     
    Bruce, Jul 29, 2003
    #4
  5. "Graeme" <> wrote in message
    news:...
    > I am writing a simple windows matching game with cards and pictures
    > (ie "concentration"). In order to write something like this, I need
    > to be able to give each "card" a random position in the assortment,
    > and I need the assortment to be different each time the program is
    > run. (I think) This means I need to be able to generate as many
    > random numbers at once as the number of "cards" in the assortment when
    > the program is first loaded. I have tried seeding the rand() function
    > with GetTickCount() as well as time():
    >
    > srand(GetTickCount());
    >
    > but the loop that generates the numbers is executed too fast, and I
    > end up getting 36 identical numbers. Is there a way to generate many
    > random numbers at once, and have them be different each time I run the
    > program? Possibly using recursion in the rand or srand functions?
    > Any help is greatly appreciated!
    >
    > Thanks
    > Graeme


    Use srand once at the beginning of your program, not every time you want a
    random number.

    john
     
    John Harrison, Jul 29, 2003
    #5
  6. Graeme

    David White Guest

    Yamin <> wrote in message
    news:1cnVa.79357$vz%...
    >
    > "Graeme" <> wrote in message
    > news:...
    > > I am writing a simple windows matching game with cards and pictures
    > > (ie "concentration"). In order to write something like this, I need
    > > to be able to give each "card" a random position in the assortment,
    > > and I need the assortment to be different each time the program is
    > > run. (I think) This means I need to be able to generate as many
    > > random numbers at once as the number of "cards" in the assortment when
    > > the program is first loaded. I have tried seeding the rand() function
    > > with GetTickCount() as well as time():
    > >

    > [snip]
    >
    > Hi Graeme,
    >
    > might I suggest a simpler alternative.
    > Just keep getting random numbers until you get the number of random

    numbers
    > you need. Performance should be a non-issue in such an app. Ignore
    > repeated random numbers.


    Maybe your method would be okay in this case, but there is something
    distasteful about an algorithm that gets slower and slower with each item
    chosen. When all but one item has been chosen, it could take hundreds of
    calls to rand() before it stumbles across the last one. There are some
    pretty simple methods of doing this without any retries. You can have an
    array of items and a loop that swaps every item with another, randomly
    chosen, item. That guarantees that every item will be in a randomly chosen
    position. Or you can choose a randomly chosen item on the fly, which the
    following card-dealer does.

    class CardDeck
    {
    enum { NCARDS = 52 };
    enum { NSUITS = 4 };
    public:
    CardDeck()
    {
    // start with deck in order
    for(int icard = 0; icard < NCARDS; ++icard)
    {
    deck[icard] = icard;
    }
    // make all cards available
    cards_remaining = NCARDS;
    }

    void Shuffle()
    {
    cards_remaining = NCARDS;
    }

    int DealCard()
    {
    // return error if no cards left
    if(cards_remaining == 0)
    return -1;
    int icard = rand() % cards_remaining;
    int card = deck[icard];
    deck[icard] = deck[cards_remaining-1];
    deck[cards_remaining-1] = card;
    --cards_remaining;
    return card;
    }

    private:
    int deck[NCARDS];
    int cards_remaining;
    };

    To start with a fresh deck at any time, call Shuffle(). Then each successive
    call to DealCard() will return a different card with only one rand() call
    needed.

    DW
     
    David White, Jul 29, 2003
    #6
  7. Graeme wrote:
    >
    > I am writing a simple windows matching game with cards and pictures
    > (ie "concentration"). In order to write something like this, I need
    > to be able to give each "card" a random position in the assortment,
    > and I need the assortment to be different each time the program is
    > run. (I think) This means I need to be able to generate as many
    > random numbers at once as the number of "cards" in the assortment when
    > the program is first loaded. I have tried seeding the rand() function
    > with GetTickCount() as well as time():
    >
    > srand(GetTickCount());
    >
    > but the loop that generates the numbers is executed too fast, and I
    > end up getting 36 identical numbers.


    I am pretty sure you made a common newbie mistake:

    You called srand() every time before you called rand().

    Don't do that! Call srand only once, eg. when your program starts
    up.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 29, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. August1
    Replies:
    4
    Views:
    7,796
    August1
    Dec 8, 2003
  2. David Shaw

    Why isn't srand working?

    David Shaw, Mar 3, 2004, in forum: C++
    Replies:
    14
    Views:
    685
    CBFalconer
    Mar 5, 2004
  3. August1
    Replies:
    0
    Views:
    7,578
    August1
    May 16, 2004
  4. Wavelet

    one question about srand seed

    Wavelet, Dec 9, 2004, in forum: C++
    Replies:
    2
    Views:
    771
    Jack Klein
    Dec 10, 2004
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,233
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page