• Register
Log In
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...

15.2k points

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]


87.3k points

Related questions

1 vote
1 answer 13 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 Gavin 15.2k points
1 vote
1 answer 11 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.2k points
3 votes
2 answers 21 views
Problem: I am struggling with a problem for a few hours back. I know how to sort an array by using methods. I was looking for another way (perhaps a loop) to do it more efficiently. Let me put my question this way, how to sort an array in java without using sort method? Could anybody here please help to make this happen? Thanks in advance
asked Mar 24 Gavin 15.2k points
3 votes
1 answer 18 views
Problem: Hello good souls, I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the ... in java? Let&rsquo;s say I have an array Integer[] num = { 2, 4, 6, 5, 8 }; Thanks in advance for your effort.
asked Mar 23 Gavin 15.2k points
1 vote
1 answer 14 views
Problem: Hello Kodlogs, I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method? Thanks for your valuable and patient answer.
asked Mar 31 Gavin 15.2k points