Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
I do have to write a shuffle() function for the linked list. Any suggestions on the best way to do that? The reason I'm asking the stuff on linked lists is I was never too good at the data structures stuff. Arrays I'm ok with. Any ideas would be great. Thanks
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
CANEHDN said:
I do have to write a shuffle() function for the linked list. Any suggestions on the best way to do that? The reason I'm asking the stuff on linked lists is I was never too good at the data structures stuff. Arrays I'm ok with. Any ideas would be great. Thanks
Put the list data into an array, shuffle it, and put it back in the list?
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
CANEHDN said:
I do have to write a shuffle() function for a linked list. Any suggestions on the best way to do that? The reason I'm asking the stuff on linked lists is I was never too good at the data structures stuff. Arrays I'm ok with. Any ideas would be great. Thanks

Why write a shuffle function when it already exists?

Collections.shuffle
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
That works perfectly. Who knew it could be that easy. I love, love it!! Thanks

Another thing. I know this is a stupid question but how do I add the image to show underneath my name?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
CANEHDN said:
That works perfectly. Who knew it could be that easy. I love, love it!! Thanks

Another thing. I know this is a stupid question but how do I add the image to show underneath my name?

Perhaps, you should read the FAQ. :)
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
ChrisBrightwell said:
Getting a truly random number is pretty difficult.

I would hope that most of the programming world has accepted the lack of a "true" random number overall. Just use Collection.shuffle() and it'll do well enough. Keep in mind we're shuffling a deck of cards not doing cryptography.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
ChrisBrightwell said:
Getting a truly random number is pretty difficult.

Really?

screenshot24ga.png


:eek:

Seeding with time is random enough for this purpose.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
kingjr3 said:
Seeding with time is random enough for this purpose.

Have you ever:

A) Read the Bible?
B) Seen a monkey bang on a keyboard?

1st Brian 3:18 (New MacRumors Version) - "And when Adam asked the LORD about the randomness that was in the world the LORD replied, "This is a talent I cannot give to man but only to the monkeys. They shall have dominion over all random numbers and it will be for the benefit of man." Adam trusted in the LORD and gave a monkey a typewriter." *

* - This is largely believed to be the first instance of a monkey attempting to write Shakespeare. As Shakespeare had not been born yet it is believed that this passage in conjunction with 1st Brian 7:23 may hint that Shakespeare actually copied from a monkey and not the other way around.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
kingjr3 said:
Really?[/qoute]Yes, really. Every RNG we've seen at work (all Java-based) has developed "habits" that produce predictable numbers.

I've seen a few variations of that. :) Regardless, that doesn't produce a "true" random ... esp in the 10,000+ range.

Seeding with time is random enough for this purpose.
Fair enough ... I was just stating that a "true" random is incredibly difficult.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
ChrisBrightwell said:
Yes, really. Every RNG we've seen at work (all Java-based) has developed "habits" that produce predictable numbers.

I've seen a few variations of that. :) Regardless, that doesn't produce a "true" random ... esp in the 10,000+ range.

Fair enough ... I was just stating that a "true" random is incredibly difficult.


Lighten up...My post was meant to be humurous. Everyone should know generating a random number on a computer would require an algorithm, which would mean its not truly random - but deterministic. If you seed properly, you should not see patterns or habits. I would be interested to see what RNGs you are using.
 

CANEHDN

macrumors 6502a
Original poster
Dec 12, 2005
855
0
Eagle Mountain, UT
I just used the shuffle function. I'm not making it more difficult than it needs to be. You guys should play munchkin and think of it from a programming view. Separate classes for each card seems like the best option.
 

subl1me

macrumors newbie
Jan 26, 2005
26
1
you clearly don't have the correct grasp of Object Oriented technologies and design if you're still thinking that writing a separate class for each card is the best way to tackle your project..

........but you live and you learn.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
CANEHDN said:
You guys should play munchkin and think of it from a programming view. Separate classes for each card seems like the best option.

I guess I'll say it again.

I *have* played Munchkin (the box with the expansions is within arms reach of me right now) and I do program. Making some generic classes and working your way down would in fact be easier.

Most likely (and I'm not trying to be insulting at all) you don't have enough programming experience to know that the path you are taking is by far the longer one. That being said, even if you don't get the project done at all you will learn just by working on it which is always good! So I do wish you the best of luck regardless of how you choose to go about it.

And as subl1me said... you live and learn. :)
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
CANEHDN said:
I do have to write a shuffle() function for the linked list. Any suggestions on the best way to do that? The reason I'm asking the stuff on linked lists is I was never too good at the data structures stuff. Arrays I'm ok with. Any ideas would be great. Thanks

Benchmarks show that linked lists are only better than vectors when dealing with many hundreds of thousands, or millions, of objects. And even then, you typically have to have a doubly linked list.

- Heap allocation can be expensive. A vector makes less and less heap allocations as it grows, whereas a linked list makes a constant number of allocations as it grows.

- Cache locality #1. A Java Object has at least 16 bytes of overhead. A typical linked list node, with one "next" pointer will take up 20 bytes (16 byte overhead + 4 byte next pointer), wasting 80% of memory. This will spread the real data over more pages, and reduce the CPU cache effectiveness.

- Cache locality #2. A vector will place all references adjacent to each other, meaning they're on the same page. A naive linked list node class will have each node allocated from the heap as the list is grown, which might result in each node residing on different pages. Of course the refered to data object can still be anywhere.

- Iteration. It's much faster to increment an index and dereference the next slot in an array than to follow a "next" pointer.


On the other hand, a linked list, for sufficiently large data sets, can be faster at head insertion, or inserting into the middle. But, if you're always inserting to the head, then you can simply use a vector backwards. And inserting to the middle still requires traversing to the middle for a linked list.
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
Oh yeah, and typically people decide to do:

A. Have a datafile, and one Card class, with instances per datafile entry
B. Have a bazillion Card classes

based on whether or not you have to support completely new and unknown Cards entering the system at runtime. Which is quite unlikely. And even then, you can still do:

public interface Card {
...
}

// Make KnownCard instances for each definition in datafile
public class KnownCard implements Card {
...
}

Then your predefined Card objects don't take a million classes, and you can still allow dynamically made cards to enter the system via a class loader.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
MarkCollette said:
Benchmarks show that linked lists are only better than vectors when dealing with many hundreds of thousands, or millions, of objects.

...

On the other hand, a linked list, for sufficiently large data sets, can be faster at head insertion, or inserting into the middle. But, if you're always inserting to the head, then you can simply use a vector backwards. And inserting to the middle still requires traversing to the middle for a linked list.

Vectors are synchronized. So unless he is running multiple threads and need to protect the collection, it should not be used. Arrays only make sense if the size is known beforehand. ArrayList would make the most sense to me for this purpose - functionally equivalent to Vector but NOT synchronized.
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
kingjr3 said:
Vectors are synchronized. So unless he is running multiple threads and need to protect the collection, it should not be used. Arrays only make sense if the size is known beforehand. ArrayList would make the most sense to me for this purpose - functionally equivalent to Vector but NOT synchronized.

True. But I was using the words like "vector" and "linked list" instead of "Vector" and "LinkedList" because I was talking about generic concepts, not specific classes.
 

Compile 'em all

macrumors 601
Apr 6, 2005
4,131
359
:rolleyes: I would like to note that Vector has long been deprecated
and you should not use it unless you plan to run your program on
legacy VMs. Instead you should use java.util.Collections.synchronizedList.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.