Array: Initialize an array with random values between 1 and 100

Q) Initialize an array with random values between 1 and 100

Example:

First time running the program:
94 74 73 76 50 70 88 55 83 5

Second time running the program
95 14 29 97 52 99 68 86 96 5

import java.lang.Math; //importing Math class 

public class ArrayRandomSample {
  
  public void assignRandom (){
    
    int [] sampleArray= new int[10]; //declaration, creation, and assignment of array reference to a array variable
    
    for (int i=0; i<sampleArray.length; i++){
      sampleArray[i]= (int)(Math.random()*100); //generation a random number and assigning to each element in the array
      //Math.random() generates value between 0 and 1 (1 not included) therefore multiplying it by 100 will give us double digit no. 
    }
  }
}


Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: