Results 1 to 3 of 3
- 11-15-2011, 06:26 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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
- 11-15-2011, 06:28 PM #2
Re: Random number generator with range.
Why don't you take a look at the API?
Java Platform SE 6How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-15-2011, 08:12 PM #3
Member
- Join Date
- Nov 2011
- Location
- Arizona
- Posts
- 10
- Rep Power
- 0
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
Similar Threads
-
Random Number Generator with User Input
By person287 in forum New To JavaReplies: 14Last Post: 07-10-2011, 10:49 AM -
Random Phone number generator
By elecleoalune in forum New To JavaReplies: 13Last Post: 04-20-2011, 09:47 AM -
Help with Random Number Generator
By celtics in forum New To JavaReplies: 0Last Post: 03-07-2011, 08:18 PM -
Random number generator
By zerwik in forum New To JavaReplies: 3Last Post: 12-26-2010, 12:10 PM -
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04-02-2010, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks