• Register
1 vote
28 views

Problem:

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

Solution:

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,260 points

Related questions

1 vote
1 answer 595 views
595 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 18 views
18 views
Problem I want to fill an array with random value in c++ code, Can someone help me how can i do it?
asked Apr 13 Subhendu 4.5k points
0 votes
1 answer 8 views
8 views
Question: I need to create an array using a constructor, add a method to print the array as a sequence and a method to fill the array with random numbers of the type double. Here's what I've done so far: import java.util.Random; public class NumberList { ... TODO } } I'm struggling to figure out how to fill the array with the random numbers I have generated in the randomFill method. Thanks!
asked Apr 13 Atik03 26.5k points
0 votes
1 answer 11 views
11 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 5 views
5 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 Samiur1 38.4k points
1 vote
1 answer 16 views
0 votes
1 answer 1K views
1K 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 9.8k points
0 votes
1 answer 4 views
4 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 Samiur1 38.4k points
0 votes
1 answer 9 views
9 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 13k points
0 votes
1 answer 57 views