-3

As time matter the most in my code so I have asked this. Actually I have to Generate Random Integer (int) in Java Programming language million of times in a loop so only a simple difference is going to work in this case.

I have to generate random number from 1 to 6 as said inside a loop.

First :- java.util.Random class

This is taken from this taken from here.

I have also posted the Screenshot below this.

import java.util.Random;
static Random randGen = new Random();
int spots;
spots = randGen.nextInt(6) + 1;

Screenshot

Second :- Math.random() method

int n = (int)(6.0 * Math.random()) + 1;

Cast is used as this method always returns a floating point value.

As now, I think you are familiar with the things So here is my question which one is the fastest and why? Both uses other classes, Both don't have argument of minimum value like in my case its one. Random class have a argument of largest value But Math.random() Does not have that.

But on the other hand, in order to use Math.random() we have to use the cast (int) also, as the Random value should be a Integer.

  • 2
    If you've already got this loop where you're calling it millions of times, shouldn't you be able to test this for yourself? Note that the two snippets of code you've written are not equivalent - do you want a range of 1-6 or 1-10? Always focus on writing *correct* code first... – Jon Skeet Apr 09 '17 at 07:21
  • 1
    You can just profile it yourself, can't you? – Sweeper Apr 09 '17 at 07:21
  • This question qualifies as "primarily opinion-based", but I would use `random.nextInt()` because it only has to generate as many random bits as you ask for, whereas `Math.random()` will always generate a floating-point value in full 64-bit form. – gyre Apr 09 '17 at 07:22
  • @JonSkeet i don't know how to compare. – Aryan Pandey Apr 09 '17 at 07:26
  • Um, run them both the same number of times and see how long each takes? Put it this way - you've said that "time matters" so that suggests you should be able to tell the difference between something that runs fast enough and something that doesn't. If the simplest form of the code runs fast enough that it doesn't feel noticeably slow to you, why change it? – Jon Skeet Apr 09 '17 at 07:27
  • @JonSkeet ok how we can see how much time they take. – Aryan Pandey Apr 09 '17 at 07:28
  • @JonSkeet can i see in eclipse ide , as it shows how much time your code took. – Aryan Pandey Apr 09 '17 at 07:29
  • 1
    Well I'd call `System.nanoTime()` before and after the loop... but at this point we're a fair way away from the original question, and I doubt that you've taken the time to research "How do I find out how long code takes to run" in the course of this comment thread. Always do your own research before asking other people. – Jon Skeet Apr 09 '17 at 07:31
  • 1
    @JonSkeet Thanks sir once again there was a clear difference between these two , and java.util.Random clearly won!! – Aryan Pandey Apr 09 '17 at 07:45

1 Answers1

-1

I believe the fastest one is the second one (Math.random()), as you don't have to initialize a class, but I would recommed you to use the Random class, as you can specify the maximun number you want the method to go

Example:

Random rand = new Random(); rand.nextInt(10);

This will give you a number from 0 to 10. If you want another range (say for example from 4 to 12, you have to type:

rand.nextInt(8) + 4;

This will return a number from 0 to 8, and add 4, so you'll get a number between 4 and 12.

In reply to your question, I think the fastest method is the Math.random() one, but the most usefull one is the Random class one

D.G.
  • 1
  • 2
  • What you think is apparently wrong. Read the answer provided as a duplicate link, then please delete this one. – pjs Apr 09 '17 at 15:36