• Register
1 vote


Hello guys,
I have a very simple question regarding Java Programming. I’ve searched your website before posting this question but I haven't got any appropriate answer. Yes, there are a couple of answers but not as specific as I needed. I want to generate some random numbers in an array of Java programs. So, help me to fill an array with random numbers java.

Many thanks...

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


As I can understand you are wishing to have an array with a set of random numbers. There is a built-in library in java called util.java.Random generates random numbers. Without further due let’s jump into the code snippet below:

import java.util.Arrays;
import java.util.Random;
public class MyClass {
   public static void main(String args[]) {
      double[] myArr = new double[6];
      Random r = new Random();
      for (int i = 0; i < 5; i++) {
         myArr[i] = r.nextInt();
      System.out.println("Random numbers = "+Arrays.toString(myArr));

In my case, the above program generates an output

Random numbers = [6.80037662E8, 1.437212081E9, -2.002132837E9, 1.930221152E9, -1.894212271E9, 0.0]


13 9 6
94,240 points

Related questions

1 vote
1 answer 216 views
Problem: Hello Kodlogs, I am a novice in C++ programming. I am trying to learn it from a textbook. I just completed the array chapter and about to start solving the problems of this chapter but the very first problem of this chapter stopped me. The problem is ... looking forward to having a solution. Note: I can initialize and print an array by declaring and prompting for the user as well. Thanks
asked Apr 19, 2020 Gavin 15.3k points
0 votes
1 answer 10 views
I'm struggling to figure out how to fill the array with the random numbers I have generated in the random fill method. Thanks!
asked Feb 2 TeamScript 13.5k points
0 votes
1 answer 7 views
Problem: Get random number between 0 and 1 in java In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. Math.random method returns double value between o(inclusive) to 1(exclusive).
asked Dec 3, 2020 Mashhoodch 9.1k points
0 votes
1 answer 31 views
0 votes
1 answer 158 views
Problem: Hi there! I got an assignment in which I have to generate a game that works like ludo. This would be a two-player game. My program will generate a random number between 1 and 6 whenever a player presses Enter key and adds all the numbers of a user unless the ... excedes 100. I do not know how to generate random numbers between 1 and 6. I would be glad if someone could help me. Thanks!
asked Nov 10, 2020 Code Learner 5.8k points
0 votes
1 answer 22 views
Problem: I am learning java and want to know how to create a random array in java? Thanks in advance.
asked Dec 30, 2020 sasha 8.7k points
0 votes
1 answer 13 views
Problem: C++ program to generate random numbers
asked Feb 14 jasimchaudary 640 points
0 votes
1 answer 2 views
Problem: am trying to generate a random grid and to represent an empty space, non-empty space and a space where a player is I am using -1, 0 and 1 respectively in a 2D array. I want to be able to generate a random graph at run time and fill up up the grid with -1 ... it doesn't have a method where I can do something like nextInt(-1, 1), where I can set the starting range. Any help would be great.
asked 6 days ago Ali 4.4k points
0 votes
1 answer 8 views
Problem: I would like to know how to generate a random number between two given values. I am able to generate a random number with the following: Random r = new Random(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ a[i][j] = r.nextInt(); } } However, how can I generate a random number between 0 and 100 (inclusive)?
asked Feb 18 Ali 4.4k points
0 votes
1 answer 3 views
Problem: I want to generate Random Float numbers between Interval [0,1]. The generated Random values will be compared with an input from a file. The content of the File is a probability (float values) and hence they are in range[0,1]
asked Feb 18 Ali 4.4k points