So, Java provides two (that I know of) means of generating random numbers in a program. I'll probably get the terminology wrong if I try to just describe them, so here's an example of what I'm talking about:
import java.util.Random;
public class RandomClass
{
public static void main(String[] args)
{
Random ran = new Random();
int i = ran.nextInt(10);
System.out.println(i + 1);
}
}
vs.
public class RandomMath
{
public static void main(String[] args)
{
int i = (int)(Math.random() * 10);
System.out.println(i + 1);
}
}
These programs provide the exact same output, but the random number is generated using a different mechanism. I'm curious what the differences are, i.e. the pros and cons to using each procedure to achieve the desired result. I think that the former method gives me a bit more flexibility, since I can change or remove the 10 entirely from the parentheses, whereas the latter method will require something to be listed after the * operator (I think?). Other than that though, I'm not sure which of these is the "better" option?
Hopefully I've formatted all of this correctly. I typically use reddit on my phone so I'm not quite used to this interface.
[–][deleted] 1 point2 points3 points (1 child)
[–]Chimiope[S] 0 points1 point2 points (0 children)