0

I'm having trouble using the random number generator, or having trouble understanding the concept entirely. I want to generate 20 random numbers from 0 to x.

Random ranNum= new Random();
int  n = ranNum.nextInt(x) + 0;

Is this how I would go about it?

3
  • 1) That looks like Java, is that correct? If so, add it to your tags. 2) Do you want the result to be inclusive or exclusive of x? 3) What do you think is accomplished by adding zero? 4) Have you tried what you proposed? If not, why not, and if so, what did it not do that you were hoping to see? Commented Mar 24, 2016 at 21:32
  • Sorry about that, the upper bound is inclusive Commented Mar 24, 2016 at 22:08
  • I really dont know why I added the zero, I was trying it out in my program and what I wrote really didn't work. Commented Mar 24, 2016 at 22:09

6 Answers 6

1

The following will create an ArrayList of values between 0 and x, inclusive, assuming that x has been previously defined as a positive int.

ArrayList<Integer> randNums = new ArrayList<Integer>();
Random r = new Random();
for(int i = 0; i < 20; ++i) {
    randNums.add(r.nextInt(x + 1)); 
}

You can then use the values however you wish (printing or in subsequent calculations) by iterating through the ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

0

Run this code:

int x=100,n=0;
Random ranNum= new Random(); 
for(int i=0;i<20;i++)
 {
   n = ranNum.nextInt(x);
   System.out.println(n);
 }

X is the range of random number. this program generate random number between 0 to 100.If you want to specify a min value than you can try this line inside the loop:

n = ranNum.nextInt(x-min)+min;

Comments

0

With this you create 20 random numbers. I still didn't understand wheter you wanna have duplicates in your random numbers or not. Well, try this:

static Random r = new Random();
private static final int number = 5;

public static void main(String[] args) {
    for(int i =0; i<20;i++){
        System.out.println(r.nextInt(number));
    }

}

Comments

0

Depends on what you need the numbers for.

To print them, you would:

Random randNum = new Random();
    for(int x = 0; x < 20; x++){
        System.out.println(randNum.nextInt(20));
    }

Put in ArrayList and print out:

    ArrayList<Integer> list = new ArrayList<Integer>();
    Random randNum = new Random();
    for(int x = 0; x < 20; x++){
        list.add(randNum.nextInt(x)); //must set x to set maximum number it can be
    }
    for(int a : list)
        System.out.println(a);

Comments

0

I like Math.random(), it returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

public int getNumber(int max) {
    return (int) (Math.random() * max); //returns an int between 0 and max
}

public void main() {
    for(int i = 0; i < 20; i++) {
        System.out.println(getNumber(100));
    }
}

Prints 20 numbers between 0 and 100, for more info about Math.random() https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

5 Comments

Your first statement is wrong, as is the comment in your getNumber method. The upper limit for Math.random() is not 0.9, and multiplying by max and casting to int will never yield max.
yes. if you need numbers between 1-10, use (Math.random() * 10) + 1. if you want numbers between 0-9, use (Math.random() * 10), if you want numbers between 0-10, use (Math.random() * 10 + 1) + 1. where 10 is max. Thats why I've placed it in a method. Don't know what you want
Using nextInt would be better, that's what it's for. Noting within your answer that things need to be handled differently depending on whether the upper bound is inclusive or exclusive would make it a better answer. Not putting in false statements such as Math.random() "returns a double between 0.0 and 0.9" would be best of all.
regarding my statement. "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." from the api doc.
Then say that. What your answer currently says is that you won't get numbers between 0.9 and 1.0, when in fact 10% of the results will fall in that range.
0
    for (int i = 0; i < 20; i++) {
        double r = Math.random();
        int[] RandomNumbers= new int[20];
        RandomNumbers[i] = (int) (101. * r);
        System.out.println(RandomNumbers[i]);
    }

1 Comment

Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.