How to sum int random number and double random number? - java

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);

Related

Select random number and add to ArrayList

So for example I have a small piece of code that looks like this...
public int random1;
public int random2;
List<Integer> listOfNums =Arrays.asList(1,2,3,4,5,6)
ArrayList<Integer> numberSelected = new ArrayList<Integer>();
Public ArrayList<Integer> selectNums{
random1 = ...
random2 = ...
}
What would be the quickest and cleanest way of having random1 and random2 select a random number from listOfNums and then putting that number in the numberSelected ArrayList?
As simple as this:
numberSelected.add(listOfNums.get(random1));
numberSelected.add(listOfNums.get(random2));
Ensure that random1 and random2 are within the bounds of listOfNums.
var random1 = Math.floor(Math.random() * 6); //Generate a random number in [0,5]
numberSelected.add(listOfNums[random1]); //Add listOfNums[random1] to numberSelected
listOfNums.splice(random1, 1); //Remove listOfNums[random1] from listOfNums
var random2 = Math.floor(Math.random() * 5); //Generate another random number in [0,4]
numberSelected.add(listOfNums[random2]); //Add listOfNums[random2] to numberSelected

NaN error for gaussian random distribution

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)

How to generate a random number between two values then set to TextView?

How to generate a random number between two values and then set to TextView?
int min = 1;
int max = 100;
Random r1 = new Random();
int random = r1.nextInt(max - min) + min;
tv1.setText(""+random);
Random random = new Random();
int value = random.nextInt(max - min) + min;
num1.setText(String.valueOf(value));
barwnikk was almost right, but shouldn't be doing that kind of tricks in order to convert from int to String
Random random = new Random();
int value = random.nextInt(max - min) + min;
num1.setText(value+"");
You can't use .setText(value), because integer will be a resource link to /res/strings! Add +"" after number!!!
There are two (or more) methods:
public final void setText (CharSequence text) - To set text
public final void setText (int resid) - to set text from file /res/string/strings.xml
This is how to generate a number between two numbers but i don't know how to set to text view
Random r = new Random();
int value;
value = r.nextInt(max - min) + min;

How to call if statements on arrays in java

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);

Random seed Math.random in Java

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;
}
}

Categories

Resources