Random Order for Numbers

Ok, so I am trying to make a card game, and want to make a way to shuffle cards. What I am looking for is how to generate the numbers 1 to 52 (1 for each card) in a random order and then have them separated to different variables.

Comments

  • : Ok, so I am trying to make a card game, and want to make a way to shuffle cards. What I am looking for is how to generate the numbers 1 to 52 (1 for each card) in a random order and then have them separated to different variables.
    :
    You'll need to generate random number between 1 and 52. However, the RND instruction may give you the same number again, so you've got to check for duplicates. See if the following Quickbasic code helps.

    DECLARE FUNCTION RandInt% (lower, Upper)
    RANDOMIZE TIMER
    DIM duptab (1 to 52)

    DO
    randnum = RandInt% (1, 52)
    if duptab(randnum) = 0 then
    duptab(randnum) = 1
    EXIT DO
    end if
    LOOP

    rem At this point you have a unique random number from 1 to 52.

    END
    ' ======================= RandInt% =============================
    ' Returns a random integer greater than or equal to the Lower parameter
    ' and less than or equal to the Upper parameter.
    ' ==============================================================

    FUNCTION RandInt% (Lower, Upper) STATIC
    RandInt% = INT(RND * (Upper - Lower + 1)) + Lower
    END FUNCTION

  • : Ok, so I am trying to make a card game, and want to make a way to shuffle cards. What I am looking for is how to generate the numbers 1 to 52 (1 for each card) in a random order and then have them separated to different variables.
    :
    One way, that isn't as ellegant as the post before me, is to do a reverse sorting function, set up the cards in order, then run a loop a hundred times that swaps two cards. The result should be more real, couse when I sort, I don't do it good, and this algorithm is also bad...

    The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • : : Ok, so I am trying to make a card game, and want to make a way to shuffle cards. What I am looking for is how to generate the numbers 1 to 52 (1 for each card) in a random order and then have them separated to different variables.
    : :
    : One way, that isn't as ellegant as the post before me, is to do a reverse sorting function, set up the cards in order, then run a loop a hundred times that swaps two cards. The result should be more real, couse when I sort, I don't do it good, and this algorithm is also bad...
    :
    : The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    : Your card shuffling method is fine, except for the part that says, "set up the cards in order." If you do this, then the final order of the cards after looping 100 times, will be shuffled, but every time you run the program you will get the same shuffled order. If the order or the cards change during the game, then just apply your loop 100 times logic to whatever order they are in. Then it will work.

    Because of the RANDOMIZE TIMER instruction in my method, you will get different random numbers on each program run. The RND instruction uses the TIMER to select random numbers. The RANDOMIZE TIMER insures that they be different.
    *****

  • : : One way, that isn't as ellegant as the post before me, is to do a reverse sorting function, set up the cards in order, then run a loop a hundred times that swaps two cards. The result should be more real, couse when I sort, I don't do it good, and this algorithm is also bad...
    : :
    : : The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    : :
    : : Your card shuffling method is fine, except for the part that says, "set up the cards in order." If you do this, then the final order of the cards after looping 100 times, will be shuffled, but every time you run the program you will get the same shuffled order. If the order or the cards change during the game, then just apply your loop 100 times logic to whatever order they are in. Then it will work.
    :
    : Because of the RANDOMIZE TIMER instruction in my method, you will get different random numbers on each program run. The RND instruction uses the TIMER to select random numbers. The RANDOMIZE TIMER insures that they be different.
    : *****
    :
    :
    I wrote: ,then run a loop a hundred times that swaps two cards.
    I didn't say the swaping process shouldn't be random.

    The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • : : : One way, that isn't as ellegant as the post before me, is to do a reverse sorting function, set up the cards in order, then run a loop a hundred times that swaps two cards. The result should be more real, couse when I sort, I don't do it good, and this algorithm is also bad...
    : : :
    : : : The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    : : :
    : : : Your card shuffling method is fine, except for the part that says, "set up the cards in order." If you do this, then the final order of the cards after looping 100 times, will be shuffled, but every time you run the program you will get the same shuffled order. If the order or the cards change during the game, then just apply your loop 100 times logic to whatever order they are in. Then it will work.
    : :
    : : Because of the RANDOMIZE TIMER instruction in my method, you will get different random numbers on each program run. The RND instruction uses the TIMER to select random numbers. The RANDOMIZE TIMER insures that they be different.
    : : *****
    : :
    : :
    : I wrote: ,then run a loop a hundred times that swaps two cards.
    : I didn't say the swaping process shouldn't be random.
    :
    : The one and only [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    :
    Guys:
    [code]
    for n=1 to 52 do {
    tempbyt=rnd(52)+1;
    tempcard=card[n];
    card[n]=card[tempbyt];
    card[tempbyt]:=tempcard
    }
    [/code]
    As you can see it is not in BASIC and not in any other language though I dunno what elements you would like to implement in the prog, and the way you want to do it. I hope you find it flexible enough and that you are able to convert it into BASIC easily.

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories