How do I create a java random number generator that creates 3 numbers using seeds

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m trying to create a random number generator with java that and output 3 numbers from 1-8 using seeds from user input, such as a user inputs 1 as a seed it sets out a set of 3 numbers, then sets out another 3 numbers, if the user inputs another seed such as 4 then it will give a different set of numbers?

I know how to use scnr.nextInt(); for the user input for the seed, but how do I use it for the seed and number generator?

Answers

You can always use the Random class in Java.

Random.setSeed();
Random.nextInt();

Keep in mind that if you wanted to just get 3 random numbers without needing to reproduce your results, the seed is really useless; just calling Random.nextInt(8) will give you a number between 0 and 8 (Inclusive of 0, Exclusive of 8).

If you really want to use the seed, you ll need to create a Random object first.

Random x = new Random();
x.setSeed(userInput);
x.nextInt(8);

This will only generate a single random int, so a good idea may be to make it so that the user must input at least 3 integers that you can run through (if you want a set of 3 numbers).

eg: user input (usrInput) is 123

x = usrInput % 10;
usrInput = usrInput/10;
y = usrInput % 10;
usrInput = usrInput/10;
z = usrInput % 10;

//generate Random numbers using x y and z as 3 seeds.
...

EDIT: Upon thinking about this a bit more, i just realized that you could actually just call nextInt(8) 3 times. The seed sets the starting point of the generator, so you don t actually need the 3 user inputs, just one will do fine.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/27917444/how-do-i-create-a-java-random-number-generator-that-creates-3-numbers-using-seed

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils