6 Replies - 1095 Views - Last Post: 19 June 2012 - 07:44 AM Rate Topic: -----

#1 SmilesNLulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 12

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?


   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++;
            }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Java random number calling method inside method

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

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.
Was This Post Helpful? 1
  • +
  • -

#3 SmilesNLulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 12

Re: Java random number calling method inside method

Posted 17 June 2012 - 02:30 PM

View Postsepp2k, 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++;
            }
    }


Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

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.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

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
Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Java random number calling method inside method

Posted 18 June 2012 - 06:31 AM

View Postpbl, 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

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 :)

This post has been edited by pbl: 19 June 2012 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1