Random number generator with range.
Hi
Really struggling with this!
As part of an assignment for uni I have been given a code that produces a random number. I have been asked to amende the code so that it print a random integer in the range 1 to 1000.
The code I have been given is:
import java.util.Random;
public class RandomSample
{
public static void main(String args[])
{
int n=6;
int value;
Random num_gen = new Random();
value = num_gen.nextInt();
value = Math.abs(value);
value = value%n;
value++;
System.out.println(value);
}
}
Does anybody have a clue what changes I need to make to put the range in?
Thanks in advance! x
Re: Random number generator with range.
Why don't you take a look at the API?
Java Platform SE 6
Re: Random number generator with range.
Here is a quick article on Random with a range specified. Try it out and see if you can get it to work.
Java: generating random number in a range - Stack Overflow