I am new to java and trying to figure out how to generate a random number from 0.000-1.000 using a seed which will be inputted by the user. If someone can show some example code it would help a lot. Thanks in advance.
Asked
Active
Viewed 916 times
0
-
1possible duplicate of [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) – Oct 08 '14 at 03:48
-
so what's your problem? you don't know how to generate random number? Or dunno how to provide seed to random generator? Or dunno how to generate random for specific range? Or dunno how to get user input? – Adrian Shum Oct 08 '14 at 03:53
-
possible duplicate of http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java – Malwinder Singh Oct 08 '14 at 04:17
2 Answers
1
You can try:
System.out.println("Type a number");
Scanner sc = new Scanner(System.in);
int seed = sc.nextInt();
Random random = new Random(seed);
double randomNumber = random.nextDouble();
System.out.printrln("The random number is: " + randomNumber);

Jean Logeart
- 52,687
- 11
- 83
- 118
1
The Java Random class allows for seeds. You can instantiate it with a seed and also change the seed on the fly.
Random rangen = new Random(123456789);
int result = rangen.nextFloat(); // Returns double between 0.0 and 1.0;
String seedStr = "This String can be used for a seed by using the Hash Code";
rangen.setSeed(seedStr.hashCode());

Nick Alexander
- 1,543
- 1
- 14
- 26
-
1Answer containing only reference to external links will become obsolete if those links ever get broken. Please add some description and sample codes on how to solve the problem. http://stackoverflow.com/help/how-to-answer – ivan.sim Oct 08 '14 at 04:25