File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Random number generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random number generation" Watch "Random number generation" New topic

Random number generation

Matt Hall

Joined: Feb 28, 2006
Posts: 24
I would like to write a method that can distribute unique integers at random between 4 arrays. (i.e. a deck of cards). Could anybody give me any ideas on how to do this? Cheers
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249


First let us know what you have done till now???

This is not a DirectSoultion Provider forum first let us know then we can rectify that what more can we do??
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well, if your wanting to create a random number between 1-13(13 cards in each suit).

u use a simple random generator, but you would have to keep track of the

random numbers generated, as to not have doubles.

or if your wanting to just load the arrays, with numbers 1 - 13,

you could use a for loop.

like Ankur said, we can't help if we don't know what exactly you are trying

to do.

[ May 04, 2006: Message edited by: Justin Fox ]

You down with OOP? Yeah you know me!
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
I think the easiest way would be to create a shuffle method. There are many ways to accomplish this easily, just think about what is going on when a real deck is being shuffled. Yes, random numbers are involved, but not random numbers over the entire deck.

Once that is done, a deal method can easily deal each card off the top of the deck.
[ May 04, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Matt Hall

Joined: Feb 28, 2006
Posts: 24
OK I have the following code

int DHand1[] = new int[13];
int DHand2[] = new int[13];
int PHand1[] = new int[13];
int PHand2[] = new int[13];

public void deal();
for (int k = 0; k< 52; k++)
Random RNum = new Random();

Random RNum = new Random();
int start;
start = 1 + RNum.nextInt(51);

This is with the java.util.Random class imported, this should generate the random integers, however i am unsure of 2 things, the first one is how to make the integers unique from each other, the second is how to spread the deal across the 4 arrays. I think the random integers can be assigned to an array with
DHand1[k] = start;
But i dont know how to put the first 13 numbers in DHand1, then the next 13 in DHand2 etc.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Think again about Rusty's shuffle idea. You start with 52 cards in a new deck in order, then move them about in random ways. If you start with your arrays filled in order you don't have to worry about duplicates and you can focus on moving them about. How would you make sure each card gets moved at least once to some other position in the array?

BTW: The 4 arrays for 4 suits may be exactly right for your problem but they would be a pain in most normal card playing problems. To put all the cards in one array you'd need to know the number and the suit of the card in each slot but your int array only holds one number in each slot. That sounds like a job for a little tiny object with two fields to me.

Try these ideas in some code and let us know how it goes!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Random number generation
It's not a secret anymore!