Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to randomize an array java

void shuffle(int [] arr) {
        for (int i = 0; i < arr.length; i++) {
            int index = (int) (Math.random() * arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
    }
Comment

java random array

import java.util.Random;
public class Example {
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object
      int[] arr = new int[5];
      for (int i = 0; i < arr.length; i++) {
         arr[i] = rd.nextInt(); // storing random integers in an array
         System.out.println(arr[i]); // printing each array element
      }
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: literals in java 
Java :: java text file to arraylist 
Java :: contains hashmap java 
Java :: Validation failed for query for method public abstract java.util.List 
Java :: how to add 2 numbers in java 
Java :: convert array of int to arraylist java 
Java :: android list[i] 
Java :: java how to make a number 
Java :: arrays.tostring java 
Java :: caused by: java.lang.noclassdeffounderror: org/springframework/boot/configurationprocessor/json/jsonexception 
Java :: find min in array java 
Java :: multiple type array java 
Java :: arraylist items into string 
Java :: string.join java 8 
Java :: How to generate all possible IP addresses based on a given set of digits, in Java? 
Java :: max int in java 
Java :: how to declare and insert value to arraylist in same line in java 
Java :: java leap year 
Java :: spring boot actuator 
Java :: milliseconds to seconds java 
Java :: remove first and last character from string in java 
Java :: Adding in java using user input 
Java :: How to find the logged-in user in Spring Boot? 
Java :: min max heap java 
Java :: java enum length 
Java :: java get length of a string 
Java :: creating a directory using java 
Java :: declare class constant array java 
Java :: brew use java 11 
Java :: android java Check Wifi Connection 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =