Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random number generation

 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt,

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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-Justin-
[ May 04, 2006: Message edited by: Justin Fox ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic