0

How would I scale random numbers to say 1-10 whithout loosing the randomness?

I know, just using % 10 is not a good idea.

And in addition, I need a seed value, so I can test on determinsitic numbers,

but I have not understood what value this seed should have and I how it influences the sequence.

Is there a difference if it is high or low ?

thanks in advance.

jam
  • 1,253
  • 1
  • 12
  • 26
  • PRNGs produce a very long sequence that repeats over and over. The seed determines your starting position in that sequence. Sequence is so long that not knowing the seed = not knowing what comes next. – zapl May 31 '16 at 17:40
  • @zapl so if the sequence is so long, I do I know when It would repeat ? – jam May 31 '16 at 17:43
  • after quite a while: http://stackoverflow.com/a/10217354/995891 - and since the beginning of the sequence doesn't look any different from some randomly similar sequence, you're not even easily able to tell that you're seeing the same thing from the start, not unless you keep track of every number generated or so. – zapl May 31 '16 at 17:50

2 Answers2

3
import java.util.Random;

Random random = new Random( yourSeed );
int randomInt = random.nextInt(10) + 1 ;

The seed has no other purpose than to give the PRNG a starting point. It is purely arbitrary and will not influence the distribution of the random numbers.

The nextGaussian() is different in that it returns floating point numbers distributed around a bell curve.

Stavr00
  • 3,219
  • 1
  • 16
  • 28
0

usually when trying to get a random value you'll use something like this:

public int getRandom( int min, int max )
{
    return (int) Math.round( Math.random() % (max - min) + min );
}

Or, as I just remembered from Stavr00's answer, use one of the built in functions from java.util.Random

Gelunox
  • 772
  • 1
  • 5
  • 23
  • Doesn't work like that. `Math.random()` produces only values 0..1, you'll have to multiply like in http://stackoverflow.com/a/363732/995891 – zapl May 31 '16 at 17:48