CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2008
    Location
    UK
    Posts
    35

    Multiple Random Sequences

    My problem is that I want to be able to produce repeatable sequences of random numbers.

    My simplified example of what I want to accomplish:

    Poker Game: I want to be able to shuffle the deck & deal the cards to 10 players. play the hand/game then be able to repeat the game, but moving each player around the table. Therefore everyone gets to play everyone elses hand.

    Now this seems obvious to just seed using a known number, then reseed it to produce the same sequence. But my problem is that while playing any given hand I may use rand() an arbitrary number of times, in between dealing the cards. therefore the next time I use rand() it may be at sequence index[30] or index[42], which will then produce different numbers.

    Is there anyway using rand() or some other random library to be able to pick and choose different sequences to randomize. Like keep sequence A for shuffling Cards and sequence B for other random uses.

    I know the idea can be done, as I have used it in the 3d modelling software Maya, using its MEL command syntax ( annoyingly I know it's even written in C++ ):

    http://download.autodesk.com/us/maya...ands/rand.html

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Multiple Random Sequences

    Use two or more random number generator function objects. Such facilities are available from TR1 and Boost, or you can implement say, Mersenne Twister yourself into your own random number generator class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Multiple Random Sequences

    Why regenerate the numbers? Just create a vector or list. Initialize it with the cards. Call random_shuffle to generate a random sequence. Each time you deal, just start at a different point in the array or list and wrap around to the beginning when you reach the end, or if you use a list, just remove the head, make it the new tail, then redeal.

  4. #4
    Join Date
    Oct 2008
    Location
    UK
    Posts
    35

    Re: Multiple Random Sequences

    GCDEF: tht concept works, but then plan is to generally play a whole game possibly 100 hands, see who the winner is for the whole game. then repeat the game, and see who the winner is (hopefully the same player).

    if i wanted to utilise the same randomized set of cards then redeal them each hand, i'd have to have 10 games running concurrently keeping track of all different chip counts, hand, opponent weight tables etc... not very efficient for something as simple as multiple number sequences?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Multiple Random Sequences

    Quote Originally Posted by purebuu View Post
    GCDEF: tht concept works, but then plan is to generally play a whole game possibly 100 hands, see who the winner is for the whole game. then repeat the game, and see who the winner is (hopefully the same player).

    if i wanted to utilise the same randomized set of cards then redeal them each hand, i'd have to have 10 games running concurrently keeping track of all different chip counts, hand, opponent weight tables etc... not very efficient for something as simple as multiple number sequences?
    That doesn't make sense to me. You say you need the same sequence of cards repeated. How is regenerating the random sequence any different from reusing the original sequence? Why not just store copies of the original sequences then?

  6. #6
    Join Date
    Oct 2008
    Location
    UK
    Posts
    35

    Re: Multiple Random Sequences

    i said in my simplified example: that i only need to reuse card sequences for a particular hand, but really i need a reuseable sequence for a whole game.

    a whole game i'd like to repeat many times, modifying a few starting positions. I "could" store this sequence as i use it for game 1: then reuse it for game 2, game 3... and so on. but game 1 may be over in 10 hands, game 2 may go on for 100 hands: game 3 may last 50 hands.

    Thats wasted copies of sequences I may never need. Its a possible solution, but i guess having a way to generate the same sequence over n over is just simpler to implement.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Multiple Random Sequences

    Quote Originally Posted by purebuu View Post
    My problem is that I want to be able to produce repeatable sequences of random numbers.
    You get that by seeding the random generator.

    A seed is a number and each seed will produce the exact same sequence of random numbers.

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Multiple Random Sequences

    Quote Originally Posted by _uj View Post
    You get that by seeding the random generator.

    A seed is a number and each seed will produce the exact same sequence of random numbers.
    Exactly! This is why you typically see a random number generator seeded with the result from a call to 'time' (a different seed is used for every run). Just seed your generator with the same number every time.

    Viggy

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Multiple Random Sequences

    Quote Originally Posted by _uj
    You get that by seeding the random generator.
    Quote Originally Posted by MrViggy
    Just seed your generator with the same number every time.
    Eh, but purebuu stated that in the original post:
    Quote Originally Posted by purebuu
    Now this seems obvious to just seed using a known number, then reseed it to produce the same sequence.
    The way I interpreted the original problem was that purebuu wanted multiple random sequences that could be repeated separately yet used simultaneously, and if so, using random number generator objects seems like the right approach since they can be seeded separately.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured