public void swap(int numb1, int numb2)
{
if(numb1 < redblue.size() && numb2 < redblue.size() && numb1 >= 0 && numb2 >= 0){
Card temp = redblue.get(numb1);
redblue.set( numb1, redblue.get( numb2 ) ) ;
redblue.set( numb2, temp ) ;
}
else{
System.out.println("# too high");
}
}
/**
*
*/
public void shuffle()
{
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(redblue.size());
int counter = 0;
while(counter <= TIMES_TO_SHUFFLE)
{
swap(randomNumber, randomNumber);
counter++;
}
}
Java random number calling method inside method
Page 1 of 16 Replies - 642 Views - Last Post: 19 June 2012 - 07:44 AM
#1
Java random number calling method inside method
Posted 17 June 2012 - 01:41 PM
So i want to shuffle a deck of cards using these 2 methods 100 times but when i execute it does nothing. When i test the shuffle method by plugging in actual number without the while loop it works but when i add a counter and randomNumber variables it doesnt work. Does it have something to do with randomNumber variable?
Replies To: Java random number calling method inside method
#2
Re: Java random number calling method inside method
Posted 17 June 2012 - 02:00 PM
swap(randomNumber, randomNumber);
You're calling swap with two equal arguments (you're using the same variable for both arguments), so it will swap the given index with itself, which won't do anything.
If you want to swap two random indices, you'll need to call nextInt twice.
#3
Re: Java random number calling method inside method
Posted 17 June 2012 - 02:30 PM
sepp2k, on 17 June 2012 - 02:00 PM, said:
swap(randomNumber, randomNumber);
You're calling swap with two equal arguments (you're using the same variable for both arguments), so it will swap the given index with itself, which won't do anything.
If you want to swap two random indices, you'll need to call nextInt twice.
i understand that now but even when I add another it still doesnt shuffle
public void shuffle()
{
Random randomGenerator = new Random();
int randomNumber1 = randomGenerator.nextInt(redblue.size());
int randomNumber2 = randomGenerator.nextInt(redblue.size());
int counter = 0;
while(counter <= TIMES_TO_SHUFFLE)
{
swap(randomNumber1, randomNumber2);
counter++;
}
}
#4
Re: Java random number calling method inside method
Posted 17 June 2012 - 02:37 PM
This is better since it actually swaps two different places. If TIMES_TO_SHUFFLE is an odd number, you should see that two elements will actually swap their position, so something actually changes.
However since the calls to nextInt are outside the loop, they will only happen once. So your code picks two random positions and swaps those two TIMES_TO_SHUFFLE times. Since you'd rather want to swap two different positions each time, you should pick two new random places each time through the loop, so the picking of the random numbers should happen inside the loop.
PS: If you want to know how to properly shuffle an array, you should Google for the Fisher-Yates shuffle.
However since the calls to nextInt are outside the loop, they will only happen once. So your code picks two random positions and swaps those two TIMES_TO_SHUFFLE times. Since you'd rather want to swap two different positions each time, you should pick two new random places each time through the loop, so the picking of the random numbers should happen inside the loop.
PS: If you want to know how to properly shuffle an array, you should Google for the Fisher-Yates shuffle.
#5
Re: Java random number calling method inside method
Posted 17 June 2012 - 07:02 PM
Actually shuffling an ArrayList of Cards object to remove randomly Cards fronm it is a useless exercise
Simply create the ArrayList and randomly remove Cards from it
http://www.dreaminco...snippet3176.htm
Simply create the ArrayList and randomly remove Cards from it
http://www.dreaminco...snippet3176.htm
#6
Re: Java random number calling method inside method
Posted 18 June 2012 - 06:31 AM
pbl, on 18 June 2012 - 04:02 AM, said:
Actually shuffling an ArrayList of Cards object to remove randomly Cards fronm it is a useless exercise
No, it's not. Also what makes you think he wants to remove cards from it?
Quote
Simply create the ArrayList and randomly remove Cards from it
Unless you're only removing a few cards from a large deck, this is hopelessly inefficient. If you're eventually going to remove all of the cards (as would be the case if you, for example, want to deal the cards to the players), this approach would have quadratic runtime. A shuffle would have linear runtime in the same amount of code.
#7
Re: Java random number calling method inside method
Posted 19 June 2012 - 07:44 AM
May be if you have few thousand elements in you ArrayList
In that case use an array and save it's size
When you retreive a card
int index = ran.nextInt(size);
card = array[index];
array[index] = array[--size];
If you do not retreive you might be right ... I would have to run bechmnarks
In that case use an array and save it's size
When you retreive a card
int index = ran.nextInt(size);
card = array[index];
array[index] = array[--size];
If you do not retreive you might be right ... I would have to run bechmnarks
This post has been edited by pbl: 19 June 2012 - 07:45 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|