-2

I am using java.util.random with a seed(new Random(System.currentTimeMillis())

  • I need to convert this to a int(my business logic)
  • I tried using the .nextInt(), but it doesn't help

//my business logic--Below code is in a loop and is intended to generate a different random number each time//

int randomNumber=(int) Math.floor(inputParam1 * (new Random(System.currentTimeMillis())).nextInt()));

Expected Ouput:A new random number to be generated each time, in int format

Actual Output: - with nextInt() it is generating the same number each time - without converting to Int, I am not able to use the 'Random'
datatype with my int variable shown above

SidTechs1
  • 67
  • 4
  • 12
  • 2
    Post a complete minimal problem reproducing the issue. Tell precisely what output your expect, and what output you get instead. – JB Nizet Apr 30 '19 at 16:01
  • 1
    Why are you creating a new `Random` just for that? `Math.random()` gives you a double from 0 to 1. And why are you casting the double to an int before assigning it to a double??? – Benjamin Urquhart Apr 30 '19 at 16:02
  • Depending on how fast a single loop takes, System.currentTimeMillis() might answer the same value, in which case the value returned by nextDouble will be the same. I'd switch to getting the time in nanoseconds, and more important, to creating the Random instance just once and re-using it. – Thomas Bitonti Apr 30 '19 at 16:07
  • Please provide your expected input and output. It could be easier to provide a suggestion. However, Math.random() always gives a double from 0 to 1, so you are getting the double number. – Abdur Rahman Apr 30 '19 at 16:08
  • @BenjaminUrquhart: With Math.random, when it runs to around a 10000 iterations, it tends to repeat , which is why I thought of using util.Random – SidTechs1 Apr 30 '19 at 16:47
  • [`Math.random()` uses `java.util.Random` internally](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/Math.java#l745), so that doesn't solve the problem – Benjamin Urquhart Apr 30 '19 at 16:48
  • @AbdurRahman jb nizet :Updated expected input and output – SidTechs1 Apr 30 '19 at 16:49
  • @ThomasBitonti: Trying the naneseconds approach now – SidTechs1 Apr 30 '19 at 16:50

3 Answers3

1

Don't create a new instance of Random each time you want to generate a Double.

You can create one instance and then call it whenever you need a new double.

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

// loop starts here

  double randomNumber = Math.floor(inputParam1 * rand.nextDouble());

// If you want an integer up to inputParam1 as it seems, you can do:

  int randomInt = (int) randomNumber;

You can also use Math.random() as someone already suggested.

Z. Kosanovic
  • 727
  • 4
  • 10
  • Chaning the nanoseconds as per one of the inputs above and also using your suggestion of declaring once and calling it multiple times.Will keep you updated – SidTechs1 Apr 30 '19 at 16:53
0

I am not sure why are you casting it to int

double randomNumber= new Random(System.currentTimeMillis()).nextDouble(); this will give a random double number between 0 and 1

user699848
  • 109
  • 7
0

Converting below code to int makes no sense.

Math.floor(inputParam1 * (new Random(System.currentTimeMillis())).nextDouble()))

And please, check this answer Using Random Number Generator with Current Time vs Without

The most important part from above link:

If you want your random sequences to be the same between runs you can specify a seed.