[opensource] Random seed

Darla Shockley shockley at cse.ohio-state.edu
Tue Mar 20 18:25:38 EDT 2007


Sorry, I was wrong about what your code does originally--you were actually 
checking to make sure the random index still has a "card" in that location. 
I should really have read more carefully.

However, that isn't at all what I'm suggesting.  The difference is that in 
your case, when you're on your last time through the outer for loop, you 
have to go through the while loop over and over and over until you get 
exactly the right random number (since there's only one non-empty "card" in 
the deck).  That's not a big deal for an array with only ~40 elements, but 
it would become more of a problem if you wanted to randomize a larger array.

Anyway, it really doesn't matter in this case, so I apologize for going on 
about it.  ...I will stop talking now.

Darla


"BRIAN SWANEY" <swaney.29 at osu.edu> wrote in message 
news:mailman.36.1174426703.1359.opensource at cse.ohio-state.edu...
> The swap thing you mentioned is, more or less, what my current algorithm 
> does. Thanks anyway though.
>
> -Brian Swaney
>
> ----- Original Message -----
> From: Darla Shockley <shockley at cse.ohio-state.edu>
> Date: Tuesday, March 20, 2007 4:01 pm
> Subject: Re: [opensource] Random seed
>
>> By "swap(arr[a],arr[b])", I just meant that as shorthand for:
>>
>> tempString = arr[a];
>> arr[a] = arr[b];
>> arr[b] = tempString;
>>
>> Sorry to not be clear!  :)  But  since that is working like you
>> want now
>> anyway, it probably doesn't matter.
>>
>> As for == and strings, in case you haven't figured out yet (and
>> since I
>> wasn't very clear before), what you should be using is
>> "string1.equals(string2)"  (or "string1.equals("abc")") instead of
>> "string1
>> == string 2" (or "string1 == "abc"").
>>
>> Darla
>>
>>
>> "BRIAN SWANEY" <swaney.29 at osu.edu> wrote in message
>> news:mailman.34.1174420174.1359.opensource at cse.ohio-state.edu...
>> > Well, I'm gradually learning that the "==" thing doesn't work
>> all that
>> > well with strings... I took my first programming class this
>> quarter (CSE
>> > 201), so I'm still learning the syntaxes by trial and error.
>> >
>> > Fixing the double variable thing seems to work (it goes all the
>> way
>> > through). Now I'm on too fixing the part where it crashes when
>> reading
>> > effect texts for certain cards.
>> >
>> > Regarding the class, I quite agree in how sloppy it is to have
>> the whole
>> > code in 1 file, but I don't yet know how to put it into another
>> class. I
>> > tried once, and it just started calling it undefined. If someone
>> knows how
>> > (which I'm sure they do), some advice on doing that would be
>> greatly
>> > appreciated...
>> >
>> > I'm not sure I really understand the "swap(a, b)" syntax too
>> well (I've
>> > only seen limited commands in intro to Java). If it repeats the
>> positions
>> > (which it does), then wouldn't that wind up swapping a bunch of
>> empty/null
>> > strings? I am using a certain code I used in my TI-83 calculator
>> that I
>> > worked on a year ago with a friend (I'm also attaching the GPL
>> to it, and
>> > attaching it in an e-mail for whoever wants a copy. Replace
>> occurrences of
>> > "-->" with an arrow, since n it's in Notepad format.). This code
>> writes a
>> > given (random) position line in the first array to a different
>> (in
>> > sequence) line in the other one, then blanks out the first one
>> so it
>> > remembers that it already took that card.
>> >
>> > As for the arrays, the TI-83 graphing calculator has no file
>> reading
>> > whatsoever, and it instead decodes the information from numbers
>> in a
>> > matrix (prgmSUBDECK4) and shuffles numbers in a list from 1 to
>> 40 (well,
>> > assuming nothing was swapped with the side deck). Since the file
>> reading
>> > seems a bit limited and redundant, each individual line from the
>> file
>> > already is stored on an string array (generally) called "deck".
>> It only
>> > ever reads from the file to set the current arrays.
>> >
>> > I thought it was a bad random seed all this time. As for the
>> pseudorandom
>> > thing, I think I'm just going to update a seed input each time
>> by writing
>> > it to a file and re-reading it the next time the file is opened.
>> >
>> > Many thanks to all of you for your help so far.
>> >
>> > -Brian Swaney
>> >
>> > ----- Original Message -----
>> > From: Darla Shockley <shockley at cse.ohio-state.edu>
>> > Date: Tuesday, March 20, 2007 12:12 pm
>> > Subject: Re: [opensource] Random seed
>> >
>> >> 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
>> >>
>> >>
>> >> _______________________________________________
>> >> Opensource mailing list
>> >> Opensource at cse.ohio-state.edu
>> >> http://mail.cse.ohio-state.edu/mailman/listinfo/opensource
>> >>
>> >
>> >
>> >
>>
>>
>> _______________________________________________
>> Opensource mailing list
>> Opensource at cse.ohio-state.edu
>> http://mail.cse.ohio-state.edu/mailman/listinfo/opensource
>>
> 




More information about the Opensource mailing list