Non-repeating random numbers?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • srussell
    Indigo Rose Customer
    • Sep 2005
    • 77

    Non-repeating random numbers?

    I'm building a card game and I need to create a list of non-repeating random numbers from 1-52

    Anyone have any ideas on this? Thanks This is what I came up with so far...

    -- TEST LOOP --
    Math.RandomSeed(52);
    nFIRSTCARD = Math.Random(52);

    local FLAG = false;
    for count = 1, 52 do
    while (FLAG ~= false) do
    FLAG = false
    Math.RandomSeed(52);
    nNEXTCARD = Math.Random(52);
    if nFIRSTCARD == nNEXTCARD then
    FLAG = true
    end
    "CARD" + count == nNEXTCARD
    end
    end
  • Worm
    Indigo Rose Customer
    • Jul 2002
    • 3971

    #2
    I played a bit with that too

    Comment

    • Corey
      Indigo Rose Staff Alumni
      • Aug 2002
      • 9745

      #3
      I wonder if Lua has something like this?

      Comment

      • TJS
        Indigo Rose Customer
        • Oct 2005
        • 524

        #4
        It looks like your on a good track by making random selections and comparing them to ensure that they're not repeats, but I'd expect that the process will waste a lot of cycles as it approaches the later card selections if you are building a game that deals all or most of the deck... i.e. a lot of random 1-52 picks will get tossed when only a few are valid.

        I'd recommend taking a look at the table actions. You could construct a table in memory with 52 records representing each card (you could even include fields for suit, value, etc.) and then randomly remove records from that table and insert them into a new table(s) to represent the hands dealt.
        Yeah right. Who's the only one here who knows the illegal ninja moves from the government?

        ()))))))))o)))))))==============================================

        Comment

        • Corey
          Indigo Rose Staff Alumni
          • Aug 2002
          • 9745

          #5
          TJS is right in terms of optimizing the function. :yes

          Comment

          • srussell
            Indigo Rose Customer
            • Sep 2005
            • 77

            #6
            Initally the game requires 40 of the 52 cards, so the basic idea is to create an array of 40 unique numbers... I did this in another program using the method below... I like the table idea of removing 12 random numbers instead of trying to generate 40 unique ones...

            Comment

            • willfreer
              Forum Member
              • Aug 2004
              • 47

              #7
              games

              Speaking of games is it possible to create a memory game in AMS? for Example drag an object to an area and have it snap inplace if it was the correct item.

              Comment

              • clueless
                Forum Member
                • Jun 2006
                • 421

                #8
                - use the current time as part of the seed to create more random numbers

                Comment

                • holtgrewe
                  Indigo Rose Customer
                  • Jul 2002
                  • 779

                  #9
                  Just a thought...

                  Place each of the 52 cards into a table.

                  Randomize to the number of cards remaining in the deck each time.
                  Reduce that number each time you use a card.

                  Table.Remove() that card from the deck (table).

                  That's certainly over-simplifying it, but a similar routine to remove the selected card from the table ought to get the creative ideas flowing.

                  Comment

                  • holtgrewe
                    Indigo Rose Customer
                    • Jul 2002
                    • 779

                    #10
                    ...here's a cryptic little program I threw together that should give a starting point for some card deck logic. The "Card Shuffle" merely clears the Listbox and allows the Math.Random to do its thing.

                    The code will need to be tested and cleaned up. I hope this gives you some ideas.

                    Good luck with your project.
                    Attached Files

                    Comment

                    Working...
                    X