I get random NaN between my outputs.
Random Temp:61.816288952756864 'F
Random Temp:NaN 'F
Random Temp:NaN 'F
Random Temp:63.674960517071916 'F
Random Temp:NaN 'F
Random Temp:62.581425292320894 'F
Random Temp:58.21928355494033 'F
Random Temp:60.00714587886456 'F
Random Temp:NaN 'F
Random Temp:60.62983167247955 'F
Random Temp:62.74961457200155 'F
Random Temp:58.50731966210792 'F
Random Temp:NaN 'F
Random Temp:55.20026299393227 'F
Here is my code:
public static double gaussRandom(double dAverage, double dStddev, Random timeRand){
double iRandom1, iRandom2;
double dRandom1, dRandom2, result;
//iRandom1 = timeRand.nextInt();
//dRandom1 = (double)iRandom1 /2147483647;
//iRandom2 = timeRand.nextInt();
//dRandom2 = (double)iRandom2 /2147483647;
iRandom1 = timeRand.nextInt();
dRandom1 = (double)iRandom1 /2147483647.0;
iRandom2 = timeRand.nextInt();
dRandom2 = (double)iRandom2 /2147483647.0;
result = dAverage + dStddev * Math.sqrt(-2.0* Math.log(dRandom1))*Math.cos(6.28318531*dRandom2);
return result;
}
public static void main(String [] args){
int i, liSampleSize;
double dAverage, dStddev, result;
System.out.println("==========================\n");
System.out.println("Gaussian numbers generator\n");
System.out.println("==========================\n");
liSampleSize = 24*4; // 24 hours every 15 mins;
dAverage = 60; //Average temp;
dStddev = 2; //standard deviation of 20;
Random myRand = new Random();
myRand.setSeed(System.currentTimeMillis());
for(i = 0; i<liSampleSize;i++)
{
result = gaussRandom(dAverage,dStddev,myRand);
System.out.println("Random Temp:"+ result +" 'F");
}
}
How can I fix this code so it doesn't generate NaN?
In your code dRandom1 can be negative, while real logarithms only take arguments from (0, +inf)
Related
Here is the code:
Random randomNumber = new Random();
System.out.println(randomNumber.nextInt(10));
Random randomNumberTwo = new Random();
System.out.println(randomNumberTwo.nextDouble());
System.out.println(randomNumber + randomNumberTwo);
The terminal says: java: bad operand types for binary operator '+'
randomNumber and randomNumberTwo are generators, so in order to add them you need to generate the next value from them:
Random randomNumber = new Random();
Random randomNumberTwo = new Random();
System.out.println(randomNumber.nextInt(10) + randomNumberTwo.nextDouble());
Here you are adding the objects of the class random numbers instead to this
Random randomNumber = new Random();
int a = randomNumber.nextInt(10)
System.out.println(randomNumber.nextInt(10));
Random randomNumberTwo = new Random();
double b = randomNumberTwo.nextDouble()
System.out.println(randomNumberTwo.nextDouble());
System.out.println(a + b);
I'm trying to randomize between these 3 variables (not range, but only between these 3 values) and store it into new variable.
int randomProductDiscount() {
int disc1 = 25;
int disc2 = 35;
int disc3 = 50;
int productDiscount = (random between disc1 or disc2 or disc3);
return productDiscount;
}
Any help would be appreciated.
Put them in an array and obtain random index:
static Random rand = new Random();
int randomProductDiscount()
{
int[] disc = {25,35,50};
return disc[rand.nextInt(disc.length)];
}
This can be used for any number of values you wish to choose randomly from.
While trying to create a Coin object class using two specific seeds passed into the object upon creation, I have noticed that when passing in the seed to an int "seed", the seed variable produces a different variable than just inputting the specific number into the random number generator. Here's some code from the Coin class:
public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
public String flip(){
String heads = "H";
String tails = "T";
boolean nextFlip = flipGenerator.nextBoolean();
if (nextFlip == true)
{
headCount++;
return heads;
}
if (nextFlip == false)
{
tailCount++;
return tails;
}
return null;
}
Here's from the file that creates and prints the Coin objects:
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
The code in that file prints out the outcome of the random flips 20 times using the 17 seed, 10 times with the 13 seed and finally 35 times with the 17 seed again. However the output is incorrect when using
public Random flipGenerator = new Random(seed);
as opposed to
public Random flipGenerator = new Random(17);
or
public Random flipGenerator = new Random(13);
Why is this happening?
Instance fields are initialized before the constructor is called.
In terms of execution order, this code:
public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
is functionally equivalent to this:
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator = new Random(seed);
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
In Java, any fields which were not explicitly initialized are given a value of zero/null/false, so you are always doing flipGenerator = new Random(0). By the time you initialize seed in your constructor, the Random object has already been created. The fact that the instance field was declared after the constructor is irrelevant, because all instance fields and their initialization expressions are executed before a constructor is invoked.
Whenever you initialize the Coin class it will use 0 as the seed. Regardless if you put the flipGenerator initialization below the constructor, it will still get called in the constructor since it is an instance variable.
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
This code is using 0 because, by the time you pass the seed, the flipGenerator was initialized with the default seed of 0.
You need to do this
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
flipGenerator = new Random(seed);
}
...
}
import java.util.Random;
public class Console {
public static void main(String[] args) {
while (3>2) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
Random rand5 = new Random();
Random rand6 = new Random();
Random rand7 = new Random();
Random rand8 = new Random();
int onenum = rand1.nextInt(2);
int twonum = rand2.nextInt(2);
int threenum = rand3.nextInt(2);
int fournum = rand4.nextInt(2);
int fivenum = rand5.nextInt(2);
int sixnum = rand6.nextInt(2);
int sevennum = rand7.nextInt(2);
int eightnum = rand8.nextInt(2);
int binary[] = {onenum, twonum, threenum, fournum, fivenum, sixnum, sevennum, eightnum};
System.out.println(java.util.Arrays.toString(binary));
How can I check if, say the first number of the binary array is one?
Currently if I run, i get an output of like {1, 0, 1} ect
You can do it by using an index to access to an element in a specific position of an array:
if (someArray[position] == something)
In your case, to check the first element, it would be:
if (binary[0] == 1)
Note:
Indices in most programming languages start with 0 so the first element would be in the index 0 of the array. The second in the index 1. And so on.
Gosh i can reduce your code so much!! Analyse and see that this code does same as yours.
while (3>2) {
Random rand = new Random();
int[] binary = new int[8];
for(int i=0;i<8;i++)
binary[i] = rand.nextInt(2);
if(binary[0] == 1)
//if first number is 1
}
You can simply do:
if(binary[0] == 1) {
//do something
}
0 is the first element in array, since the fact of Arrays is a 0 based.
Side note: You don't have to declare a new object from Random to get a number, you can use the same object like:
Random rand = new Random();
int onenum = rand.nextInt(2);
int twonum = rand.nextInt(2);
int threenum = rand.nextInt(2);
// and so on
Just do this to check if first element equals to 1:
if (binary[0] == 1)
{
//do whatever here..
}
Other issues about your codes
I realized you use while (3>2). You can use while(true) if you want it to be an infinite loop.
You only need to create one random object to create multiple random numbers. Do it like this..
.
Random rnd = new Random();
oneNum = rnd.nextInt(2);
twoNum = rnd.nextInt(2);
threeNum = rnd.nextInt(2);
You can even do it this way (using an array to store the random numbers):
Random rnd = new Random();
int[] num = new int[8];
for (int x=0; x<num.length; x++)
num[x] = rnd.nextInt(2);
In my code I use random numbers in different classes. How to define random seed? Can I define this seed for all the classes in the main code?
double rnd = Math.random();
You will probably want to use the special Random class. It gives you more control over the random numbers.
To do this you first need to create a new random object.
Random generator = new Random(seed);
Then generate a new number by
double random = generator.nextDouble();
http://docs.oracle.com/javase/6/docs/api/java/util/Random.html
public class MathRandomWithSeed {
public static void main (String args[]){
int min = 5;
int max = 100;
int seed = 5;
int random = randomNext(min, max, seed);
System.out.println("Random = " + random);
}
private static int randomNext(int min, int max, int seed){
int count = (max - min) / seed;
int random = ((int)(count * Math.random()) * seed) + min;
return random;
}
}