-1

I want to generate random number between two numbers which are multiples of 10. For example, between 100 and 500, I want to generate a uniform distribution of 110, 120 ... 490, 500.

I think it is better to use Random.nextInt() as per THIS post. But not sure how to do that.

Andy897
  • 6,915
  • 11
  • 51
  • 86

2 Answers2

1

(Random.nextInt(41)+10)*10 is the correct answer. The Random.nextInt(41) will generate numbers between 0 to 40. The Random.nextInt(41)+10 will generate number from 0 to 50.

And Hence (Random.nextInt(41)+10)*10 will generate numbers between 100 and 500.Please note that 100 and 500 are also included in the result.

1

You can use this function.

public int random()
{
     Random r=new Random();
     return (r.nextInt(41)+10)*10;
}

Read this Math.random() versus Random.nextInt(int)

Random.nextInt(n) is both more efficient and less biased than Math.random() * n

SRIDHARAN
  • 1,196
  • 1
  • 15
  • 35