Java RNG seeding - java

My question concerns the Java RNG; use the following code:
for (int s = 0; s < 600; s++) {
Random r = new Random(s);
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}
This will result in 600 random numbers being generated. I know this is a bit odd, but I require a new random number generator each time in my actual project. The seed I receive is sequential. The first random double that is generated is extremely close for any of the seeds, is this because of the linear congruential formula that is used as initialization?
The second double generated actually looks like it is actually properly random, is this safe to assume so? Is it OK practice to first generate an unused random number, and after that moment start to use it for the actual reason it was created?
Thank you in advance
EDIT:
Let me clarify:
int possibleRoutes = 7;
void handlePacket(Packet p) {
int chosenRoute = p.hash % possibleRoutes;
// ...Other code...
}
vs.
int possibleRoutes = 7;
void handlePacket(Packet p) {
Random r = new Random(p.hash);
int chosenRoute = r.nextInt() % possibleRoutes;
// ...Other code...
}
}
vs.
int possibleRoutes = 7;
void handlePacket(Packet p) {
Random r = new Random(p.hash);
r.nextInt();
int chosenRoute = r.nextInt() % possibleRoutes;
// ...Other code...
}
A guarantee is that each packet must take the same route. The packet hash is inherently sequential at the moment. There are too many possible hashes to keep any type of state to speed this up.

why do you give a special number as a seed? just leave it empty, so the Random constructor will choose a seed for you.
for (int s = 0; s < 600; s++) {
Random r = new Random();
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}
see Role of seed in random number generation

Call the default constructor which uses the nanoTime as a seed. This way you won't need to generate a new seed to create your object each iteration.
//loop through number of numbers needed
for(int i = 0; i < 100; i +)
//Calls default constructor
Random r = new Random();
System.out.println(r.nextDouble()*.5);

An alternative is to use a master random to seed all the subsiduary randoms in the loop:
Random masterRand = new Random();
for (int s = 0; s < 600; s++) {
Random r = new Random(masterRand.nextLong());
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}

Related

How to generate a certain amount of random numbers within a range in Java?

I'm trying to make a small program that allows you to generate a certain amount of numbers within a range, but it does not work as expected.
For example, if I ask the program to generate 3 random numbers between 5 and 10 it gives me 5 random numbers between 0 and 5.
private void jFillActionPerformed(java.awt.event.ActionEvent evt) {
int intInput1;
int intInput2;
int intInput3;
int i;
int RandomNumber;
intInput1 = Integer.parseInt(txtInput1.getText());
intInput2 = Integer.parseInt(txtInput2.getText());
intInput3 = Integer.parseInt(txtInput3.getText());
int ListSize = (intInput3) + 1;
Random rnd = new Random();
for (i = 0; i <= ListSize; i++)
{
RandomNumber = rnd.nextInt((intInput2 - intInput1) + 1);
fill.addElement(RandomNumber);
lstNumbers.setModel(fill);
}
Simply always add 5 (or more specifically - intInput1 in your case as it seems it's lower range value) to generated numbers so it will be in the range you need (0+5=5, 5+5=10 so it will be in range 5,10)
Here an IntStream you can later than use limit to set the amount of numbers you want.
public static IntStream numbersBetween(int from, int to){
return new Random().ints().map(e -> Math.abs(e % ((to+1) - from)) + from);
}

passing variable from random number generator class

Instead of writing:
Random randomGenerator = new Random();
for (int idx = 5; idx <= 15; ++idx){
int randomInt = randomGenerator.nextInt(1);
each time I need a random number in a function, Is it possible to just pass or call the result from a random number generator class into the function?
For Example, I have one function in particular which is receiving variables from another class
favoriteTracks(String fromExampleClass1, String fromExampleClass1again)
could I do
favoriteTracks(String fromExampleClass1, String fromExampleClass1again, Long fromRNGclass)
for clarification:
My one function "favoriteTracks" requires variables passed from "ExampleClass1".
At the same time, I want it to receive a random number as a variable (or call it, whichever is easiest). generated in another class by
public static long randomNum(){
Random randomGenerator = new Random();
for (int idx = 5; idx <= 15; ++idx){
int randomInt = randomGenerator.nextInt(1);
The simplest approach would be to encapsulate the behaviour you want in a singleton:
public class MyRandom {
public static final MyRandom myRandom = new MyRandom();
private Random randomGenerator = new Random();
public int makeRandom() {
// put your for loop here if you want, but it is not necessary
return 5 + randomGenerator.nextInt(11);
}
}
somewhere else ...
x = MyRandom.myRandom.makeRandom();
That looks like a possible solution for what you are trying to do.

Different random output when run differently

I am trying to solve a problem:-
A point is at an initial position X. It can be shifted either left or right. If it is moved either left or right with equal probability 10 times, what is the probability of it ending up in its initial position X?
I used the following java program:-
import java.util.Random;
public class ProbMain {
int left;
int right;
int error;
double middle;
public ProbMain() {
left = right = error = 0;
middle = 0.0;
}
void pushLeft() {
++left;
}
void pushRight() {
++right;
}
void push() {
int whichWay;
Random rand = new Random();
whichWay = rand.nextInt(2);
if (whichWay == 0)
pushLeft();
else if (whichWay == 1)
pushRight();
else
++error;
}
public static void main(String[] args) {
ProbMain obj = new ProbMain();
for (int b = 0; b < 10000; b++) {
for (int a = 0; a < 10000; a++) {
for (int i = 0; i < 10; i++)
obj.push();
if (obj.left == obj.right)
++obj.middle;
obj.left = obj.right = 0;
}
}
System.out.println("Error: " + obj.error);
System.out.println("Probability of middle: " + obj.middle / (10000*10000));
}
}
Weird thing is that when I run this on Eclipse I get result around 0.05 but when I run from command line I get result around 0.24. Why so? And which one is correct?
You are creating a new Random object each time you want to retrieve a random number (in the push() method) - this can lead to very poor entropy and create strange results when the program is run with different timings - usually running from eclipse will be much slower due to the attached debugger, which will yield better random results when the RNG is initialized with a time value as seed.
You should change your program to use only ONE Random instance, for example by declaring a new Random member variable and initializing it once in your ProbMain constructor.

Java Random Class not truly random?

I am trying to simulate the math puzzle I found on http://blog.xkcd.com/2010/02/09/math-puzzle/. However, the java random class is returning weird results. In the code below, the result is what is expected. The output is somewhere around .612 for the first line and between .49 and .51 for the second.
int trials = 10000000;
int success = 0;
int returnstrue = 0;
for (int i = 0; i < trials; i++) {
Random r = new Random();
//double one = r.nextDouble()*10000;
//double two = r.nextDouble()*10000;
double one = 1;
double two = Math.PI;
double check = r.nextDouble();
boolean a = r.nextBoolean();
if(a)
{
returnstrue++;
}
if(a){
if((check>p(one)) && two > one)
{
success++;
}
if((check<p(one))&& two<one)
{
success++;
}
}
else{
if((check>p(two)) && two < one)
{
success++;
}
if((check<p(two))&& two>one)
{
success++;
}
}
}
System.out.println(success/(double)trials);
System.out.println(returnstrue/(double)trials);
However, when I switch the lines of
double check = r.nextDouble();
boolean a = r.nextBoolean();
to
boolean a = r.nextBoolean();
double check = r.nextDouble();
the output is around .476 for the first number and .710 for the second. This implies that the nextBoolean() method is returning true 70% of the time in the later configuration. Am I doing something wrong or is this just a bug?
Move the instantiation of r to outside the for loop, as in:
Random r = new Random();
for (int i = 0; i < trials; i++) {
:
}
What you are doing now is creating a new one every time the loop iterates and, since the seed is based on the time (milliseconds), you're likely to get quite a few with the same seed.
That's almost certainly what's skewing your results.
So, yes, it is a bug, just in your code rather than in Java. That tends to be the case in about 99.9999% of the times when people ask that question since Java itself is continuously being tested by millions around the world and that snippet of yours has been tested by, well, just you :-)

Java/Android Biased Number Generator

I have been set a task to create a Android app in which the user chooses four numbers (1-6), I then compare it against four randomly generated numbers and then tell them how many of there numbers were correct.
My problem is that whenever I generate any numbers the first three shown are always the same, except from the last number.
Random a1 = new Random();
random1 = new ArrayList<Integer>();
for (int index = 0; index < 6; index++)
{
random1.add(a1.nextInt(5)+ 1);
}
Random a2 = new Random();
random2 = new ArrayList<Integer>();
for (int index = 0; index < 6; index++)
{
random2.add(a2.nextInt(5)+ 1);
}
This is the code I use for the random number generation, each number uses the exact same code, which makes it even more confusing, if they were all the same I could understand that because it's the same code it generates the same number or something along those lines but the last one is always different, any help would always be appreciated.
Try not create two Random instances but reuse single instance instead. May be two Randoms with close seeds produces close output.
Check if below code works for you. Code taken from http://www.javapractices.com/topic/TopicAction.do?Id=62. Modified according to your requirements.
public final class RandomRange {
public static final void main(String... aArgs) {
int START = 1;
int END = 6;
Random random = new Random();
List<Integer> first = new ArrayList<Integer>();
List<Integer> second = new ArrayList<Integer>();
for (int idx = 1; idx <= END; ++idx) {
first.add(showRandomInteger(START, END, random));
second.add(showRandomInteger(START, END, random));
}
System.out.println(first);
System.out.println(second);
first.retainAll(second);//Find common
System.out.println(first);
}
private static int showRandomInteger(int aStart, int aEnd, Random aRandom) {
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
// get the range, casting to long to avoid overflow problems
long range = (long) aEnd - (long) aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long) (range * aRandom.nextDouble());
int randomNumber = (int) (fraction + aStart);
return randomNumber;
}
}

Categories

Resources