[opensource] Random seed

Darla Shockley shockley at cse.ohio-state.edu
Tue Mar 20 13:12:29 EDT 2007


Hi.  I lurk here a lot, but now I can actually be useful:

First, yeah, it looks to me like for the side deck, you're subtracting 15 
twice.  But I don't think that's the only problem.  The other problem is 
this:  Suppose your deck size is 40.  So you're picking 40 different numbers 
(between 1 and 40).  It is very, very likely that, considering that the 
numbers are random (well, pseudorandom), you'll get the repeats of some of 
the numbers.  One way you could solve this, of course, is to check for 
repeats, and try again, but that's inefficient.  Here's what I would do (in 
pseudocode):

for (i = 1 to decksize)
    swap(deck[i], deck[random.nextInt(decksize - 1))

Another thing I noticed looking at the code:  you are comparing strings 
using "==".  It doesn't seem clear to me that you know that when you use ==, 
java is comparing the memory address of the String object, not the contents 
of the string.  This is working for you because of the way Java allocates 
memory for Strings.  If I were you, I might consider changing those =='s to 
.equals().  (Also, I don't think it's guaranteed anywhere in the specs for 
the language/in the API, so there could conceivably be an implementation for 
which your code doesn't work.  But I haven't looked carefully, so it may 
actually be guaranteed somewhere.)

Also note:  I'm not very familiar with Java 1.5, so it's possible that I'm 
completely wrong and Strings are now dealt with as primitives.  If that's 
the case, ignore the second paragraph.  (That's the kind of thing I would 
think I would have heard, though.)

Ditto on the advice to create a card class--I might just handle a deck as an 
array of Cards, though.  (But I have a tendency to over-use arrays, so 
probably you shouldn't listen to me there.)

Good luck.

Darla

"Jim Dinan" <dinan at cse.ohio-state.edu> wrote in message 
news:mailman.33.1174365301.1359.opensource at cse.ohio-state.edu...
> Hi Brian,
>
> I think that line 592 might be the source of your bug:
>
> ----
> if (sideDeck)
>  oldPosition = random.nextInt(deckLength-16)+1;
> ----
>
> It looks like decklength is already adjusted above so subtracting 16 may
> be redundant.
>
> A couple other pointers:
>
> You should consider making a Deck class rather than storing everything
> in a string array.  This class can contain the boolean "sideDeck" and
> any other deck info.
>
> You should also create a Card class that has fields for all of the card
> data.
>
> Once you have this Card class, you can store all of the cards in the
> Deck class as a list of Card objects.  I'm a bit out of date on the Java
> API so maybe someone else can suggest a better alternative, but I would
> consider using a Vector<Card> (Vector of Cards) to store the deck.
>
> Good luck,
> ~Jim.
>
> BRIAN SWANEY wrote:
>> I am working on a Java code that shuffles a deck of Yu-Gi-Oh cards (a 
>> rather complicated game; see 
>> https://www.upperdeckentertainment.com/yugioh/en/gameplay/rulebook/rulebook_v06_EN.pdf 
>> for details on the rules), and have mostly formatted the display of cards 
>> by now, but cannot shuffle the deck. I probably don't understand the 
>> random seed syntax.
>>
>> My algorithm involves listing cards from 1 to 40, randomly selecting a 
>> card from that list, copying it in sequence onto a second list, then 
>> blanking out the previous card position to note that it was already 
>> taken. It seems that no matter what I enter as the seed, it doesn't go 
>> through all the cards and only repeats things that it already took (like 
>> 32 to 35 out of 40 cards). Can anyone with a bit of spare time and 
>> acceptance of newbies look it over and give me some (appreciated) advice?
>>
>> PS - Sorry that the methods are not too organized...
>>
>> -Brian Swaney
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Opensource mailing list
>> Opensource at cse.ohio-state.edu
>> http://mail.cse.ohio-state.edu/mailman/listinfo/opensource
>
>
> -- 
> James Dinan <dinan at cse.ohio-state.edu>
>
> Graduate RA - Computer Science and Engineering
>              The Ohio State University 




More information about the Opensource mailing list