## Basic

Moderators: None (Apply to moderate this forum)
Number of posts: 4766

This Forum Only

Random Order for Numbers Posted by bryamatt on 6 Oct 2005 at 6:39 PM
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.
Re: Random Order for Numbers Posted by moneo on 7 Oct 2005 at 4:27 PM
: 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

Re: Random Order for Numbers Posted by IDK on 10 Oct 2005 at 11:56 PM
: 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 Niklas Ulvinge aka IDK

Re: Random Order for Numbers Posted by moneo on 11 Oct 2005 at 7:32 PM
: : 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 Niklas Ulvinge aka IDK
:
: 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.
*****

Re: Random Order for Numbers Posted by IDK on 12 Oct 2005 at 7:33 AM
: : 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 Niklas Ulvinge aka IDK
: :
: : 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 Niklas Ulvinge aka IDK

Re: Random Order for Numbers Posted by Gaashius on 19 Oct 2005 at 8:08 AM
: : : 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 Niklas Ulvinge aka IDK
: : :
: : : 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 Niklas Ulvinge aka IDK
:
:
Guys:
```for n=1 to 52 do {
tempbyt=rnd(52)+1;
tempcard=card[n];
card[n]=card[tempbyt];
card[tempbyt]:=tempcard
}
```

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.

****************

GAASHIUS

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic