1

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?

default locale
  • 13,035
  • 13
  • 56
  • 62
Nong Shim
  • 65
  • 1
  • 5
  • 1
    1. Create java random number generator that creates one number using seed. 2. Repeat three times. – default locale Jan 13 '15 at 08:22
  • http://stackoverflow.com/questions/21049747/how-can-i-generate-random-number-in-range-in-android-and-show-it-in-a-textview-f/37829989#37829989 see this link, it might help you... – Narendra Sorathiya Mar 23 '17 at 20:56

4 Answers4

4

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.

Aify
  • 3,543
  • 3
  • 24
  • 43
1

In java random numbers can be produced using class Random.For example:

Random randomNumbers=new Random();
int random=randomNumbers.nextInt();

This generates a random number from –2,147,483,648 to +2,147,483,647. In the above code, no seed value is given. So the program uses the current system time in milliseconds as the seed value.Thus the above statement is equivalent to:

Random randomNumbers=new Random(System.currentTimeMillis());

A specific seed always generates a identical set of random numbers.Since the time keeps changing, the sequence generated using the time of day as a seed always gives a unique set of random values.

Now how to generate random numbers within a certain range? The statement:

Random randomNumbers=new Random(2);
int random=randomNumbers.nextInt(8);

generates a random number from 0 to 7 using the seed value 2 which we can off course make an user to input.The value 8 is called scaling factor. But in most of the cases we have to shift these values in order to get he desired output.So the random numbers generated above should be shifted by 1. Thus we write a expression:

int randomNum=1+randomNumbers.nextInt(8);

Now it generates random numbers in the range 1 to 8.

In order to generate 3 random numbers in a sequence you can use a loop.

for(int i=0;i<3;i++){
  int random=1+randomNumbers.nextInt(8);
  System.out.println(random);
 }
hermit
  • 1,048
  • 1
  • 6
  • 16
0

what about SecureRandom ? you can at least define some seeds using this class.

Marc Bredt
  • 905
  • 5
  • 13
0

Use Random and the modulo operator to have the number in your range (yourRandomNumber % 8) + 1