[opensource] Random seed

BRIAN SWANEY swaney.29 at osu.edu
Tue Mar 20 15:49:27 EDT 2007

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Virtual Yu-Gi-Oh.tar.gz
Type: application/x-gzip
Size: 18636 bytes
Desc: not available
Url : http://mail.cse.ohio-state.edu/mailman/private/opensource/attachments/20070320/c56220fc/VirtualYu-Gi-Oh.tar-0001.bin

More information about the Opensource mailing list