Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:07:32+00:00 2026-06-18T09:07:32+00:00

I have a java project done for school. This is a piece of code

  • 0

I have a java project done for school. This is a piece of code which i am having a hard time to understand its logic. Please shed some light on it.

for(int i = 0; i< leftbut.length; i++){
          int randomNumber =(int)(Math.random()*leftbut.length);
             tempNum = leftbut[randomNumber];
            leftbut[randomNumber] = leftbut[i];
            leftbut[i]=tempNum;      

     }

The leftbut in this case is actually an array of 9 buttons.
This code is supposed to shuffle the 9 buttons in different positions.
I just cant understand how this code works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:07:33+00:00Added an answer on June 18, 2026 at 9:07 am

    The code generate a random permutation of the original array.

    However, note that this is biased – it does not generate all permutations in uniform distribution. This thread discusses what is the affect of this bias.

    To overcome this issue – you might want to have a look on fisher yates shuffle (which main difference is, to generate a random number in range [i,n) in each iteration instead of in range [0,n).)


    EDIT:

    You might better understand it if you encapsulate the assignments in a method:

    private static void swap(int[] array, int i, int j) { 
           tempNum = array[j];
           array[j] = array[i];
           array[i]=tempNum; 
    }
    

    Now, the code will be simpler to follow:

    for(int i = 0; i< leftbut.length; i++) {
              //choose a random index in the array
              int randomNumber =(int)(Math.random()*leftbut.length);
              //swap the element in index i with the random element chosen in the array
              swap(leftbut, i, randomNumber);
    }
    

    The idea is you swap() each element in the array with a random index. This random index is chosen randomly from the array, and its index is denoted as randomNumber.

    Since you only swap() items around, you can easily prove that the output array is a permutation of the original.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Tool to convert java to c# code I have done a project
We have a class LogManager in our Java project which looks like this: public
I have a java project in which I have Referenced Libraries. In the Referenced
I have been working on a Java project in which the reports will be
I am working on a Java project where I have an ant build, which
I have a Jenkins job which compiles and publishes our Java project to a
I have done a project in java ie a motion detection s/w using javaCV
I'm currently working on a Java project which uses Tibco rendezvous control. I have
I am almost done with one Java project that I have been developing on
I have a Java EE 6 Project in Netbeans 7 which runs fine when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.