Re: Programming Challenge
[Prev][Next][Index][Thread]
Re: Programming Challenge
I found a very efficient, very random way to do this for some card games I
made. Start with a list or matrix filled with 1 to 60 in order using a For
loop. Store the number 60 into a SIZE variable. Get a random integer between
one and SIZE. (iPart SIZE*Rand+1) Take that element for the current card,
then switch it with the last element. (I used RowSwap in a matrix.)
Subtract one from SIZE. Now you're ready for another card. Used cards are
kept at the end of the stack, beyond the range of SIZE*Rand+1, and the
remaining cards are always chosen from randomly.
You can see this in action in either Blackjck or Poker, both of which are in
Casino.85g, available from TI's program archive. In fact, get Casino anyway,
they are good games that I'm quite proud of .</shameless plug>
>Dear List,
> I am trying to make a program that will simulate a deck of cards for a
>collectable card game. I would rather lay out a set of numbers at the
>beginning (each number representing a particular card) than pick them on
>the fly. What my problem is is that I need to get a list (or a matrix, it
>doesn't matter) of numbers 1 through 60, randomly assorted, BUT no number
>appearing more than once. It may sound easy, but it is fairly difficult
>(at least I didn't see an easy way to do it.) It also has to be relatively
>quick. I could have the program check every random number it gets to see
>it if is already in the list, but that would be impractical in terms of
>speed and program size.