java random number average calculation - java

How do you find an average of a group of random numbers. For example, how would you find the average of, say you prompt the user for how many times they want to generate a random number and they entered 9 and you make the random number with random = randlist.nextInt(100) + 1;? I know you find the average by dividing the sum of the numbers by how many numbers there are, but in this case, how do you find the numbers generated by the random method and add them together and divide them by 9?

With:
double avg = 0.0;
for (int i = 0; i < nTimes; i++) {
avg += randlist.nextInt(100) + 1;
}
avg /= nTimes; //This is the final average

Related

Make java give the the highest sum of 3 numbers out of 4 given

When given 4 numbers, how do i find which 3 numbers out of the 4 will give the greatest sum.
So if given 3 2 5 5, i want java to sum 3 5 5 for a total of 13
I have been searching for about 20 minutes and all i find is how the find the highest number of the 4 numbers. I know i absolutely can just write 16 lines of code comparing the 16 different combinations but i was hoping someone could point me in a faster direction.
First find the smallest number.
So for 3 2 5 5 it would be 2. Make sure you store this.
Thus, the numbers to sum are 3, 5 and 5
To get the total sum you need to add all the numbers together so:
3 + 2 + 5 + 5 = 15
And then minus the smallest number from the sum. So 15-2 which equals 13
First find the greatest of the four numbers and keep it aside.
Then find the greatest of the remaining three numbers and keep it aside.
Then find the greatest of the remaining two numbers and keep it aside.
Then sum up the numbers kept aside and display the result.
public static void main(String args[]) {
int[] vals = new int[4];
vals[0] = 3;
vals[1] = 2;
vals[2] = 5;
vals[3] = 5;
int result = sum4(vals);
System.out.println("Sum of x+y = " + result);
}
private static int sum4(int[] nums)
{
int retVal = 0;
int lowest = Integer.MAX_VALUE;
for (int i=0; i<nums.length; i++)
{
if (nums[i] < lowest)
lowest = nums[i];
retVal += nums[i];
}
retVal -= lowest;
return retVal;
}

Calculating Average After Parsing Integers

This is for Java.
I just finished parsing each column from String to int. However, now I am having a tough time figuring out how to calculate each column and finding the average.
For example, this is what I have after I parsed each column:
int Q1 = Integer.parseInt(columns[1]);
This is what I tried to find the average, but was unsuccessful.
int Q1s = Q1;
int i;
int total = 0;
for (i = 0; i < 6; i++) {
total = total + Q1s;
}
double avg = total / Q1s;
return avg;
I know how to find the average the normal way (Example: int Q1[] = {1,2,3};)
But it is not the same for parsing an array of integers.
Any hints on how to proceed would be greatly appreciated!
P.S. I don't want the answer...I just want a direction on where to go from here. That is why I didn't put the complete code that I currently have.
Your logic to calculate total is wrong. You are not adding all the elements, what you're doing is adding the first element n times.
Assuming Q1 is your array of Strings.
for(int i = 0; i < Q1.length; ++i)
{
total += Integer.parseInt(Q1[i]);//this is what you want to do. Parse it here
}
double avg = (double) total / Q1.length;
two things:
1) you should use:
double avg = total / <ColumnCount>;
2) you need to casting, So
double avg = ((double)total) / <ColumnCount>;
double avg = total / Q1s;
Calculating average is wrong here. It should be
double avg = total / 6;
It should be divided by the total number of elements here you are
using 6 as a constant.

Randomly generate odd numbers in the range [duplicate]

This question already has answers here:
Generating an Odd Random Number between a given Range
(10 answers)
Closed 7 years ago.
I'm trying to generate odd numbers randomly. I tried this, but it generates even numbers also:
int coun=random.nextInt();
for(int i=1; i<100; i++){
if(i%2==1){
coun=random.nextInt(i);
}
}
How can I generate odd numbers randomly?
You could add 1 to even numbers
int x=(int) (Math.random()*100);
x+=(x%2==0?1:0);
or multiply the number by 2 and add one
int x=(int) (Math.random()*100);
x=x*2+1;
a lot of possible solutions.
All numbers of the form 2*n + 1 are odd. So one way to generate a random odd number would be, to generate a random integer, multiply it by 2, and add 1 to it:
int n = random.nextInt();
int r = 2 * n + 1; // Where r is the odd random number
For each random number n, there is a unique odd random number r generated (in other words, it is a bijection) - thus ensuring unbiasedness (or at least, as much unbiasedness as the function random.nextInt()).
There is 50 odd numbers between 0 and 100. To select one of them you can do
int n = random.nextInt(50);
to get the n-th odd number you can
int odd = n * 2 + 1;
Putting it all together
int odd = random.nextInt(max / 2) * 2 + 1;
One solution would be to test wheter the random integer value is odd or not. If it is not, you can add or subtract one with half probability.
Random random = new Random();
int i = random.nextInt();
if (i % 2 == 0) {
i += random.nextBoolean() ? 1 : -1;
}

Java choosing between two numbers

I'm trying to make a method where you choose 2 number in main and the method finds the highest value between does to numbers.
The program takes a number divides it by 2, if not possible to divide multiply by 3 and add 1, divide again and so on until reaching 1.
output: number 10
6 times
int count = 0;
while( number != 1){
count++;
if(number % 2 == 0){
number = number / 2;
}else{
number = number * 3 + 1;
}
}
return count;
This is what i have so far and i have no idea how to pick 2 number and finding the highest one in between those 2.
Use the java.util.Random for generating random values.
Random r = new Random();
int n1 = r.nextInt();
int n2 = r.nextInt();
If you put what you have in a method that takes an int as a parameter, you can call it twice, once using a java.util.Random - generated number, and again using a different random value. You can store the results of both calls as ints, and compare the two of them. Hope that helps!
int first = reduceNumber(r.nextInt());
int second = reduceNumber(r.nextInt());
Use the Random class to generate random numbers.
To know the maximum of them,
int max = Math.max(n1, n2);
I'm sorry. Those to numbers are pick by me in main. I think i have to use array.
So the output should be like this:
Using Scanner in main.
lowest limit: 2
highest limit: 10000000
the number 837799(method that finds the number) is the one that is divided
the most times: 524(code that counts how many times it has been divided) which i have..
That's how it's suppose to look like. So i don't think random is going to help.

How choose random number within range, but weighted toward one part of that range? (in Java)

I want to choose random numbers within a range of numbers, but with weighting towards part of that range. For example:
Choose random number between 1-10
Weight it so that 1-5 is maybe 20% more likely than 6-10
Is this possible? How would I do this?
It depends on how you want your probability distribution to look like.
For example:
Pick a random number between 1-10
If it is <= 6
Return a random number between 1-5
Else
Return a random number between 6-10
EndIf
Picks a number in 1-5 60% of the time and a number in 6-10 40% of the time.
To generate a bell curve of probabilities, roll and sum multiple dice. Then subtract the average. Re-roll if the result is negative. The more dice rolled, the more weighting.
Here's one way, wholly untested.
float sum;
do {
sum = rollThreeDice(); // returns 3 to 18, with an average is 10.5
sum -= 10.5; // Now the range is 0 to 7.5, with the lower end being more likely.
} while(sum < 0);
return sum;
Of course you can roll dice with any number of sides in order to produce the desired range. You control the frequency curve by choosing the number of dice.
Two solutions come to mind. First, run a random twice:
java.utils.Random randomGenerator = new java.utils.Random();
int random = randomGenerator.nextInt(11);
if(random <= 6) { //This is 20% more
random = randomGenerator.nextInt(5) + 1;
} else {
random = randomGenerator.nextInt(5) + 6;
}
Or if you always have a static range, you could fill an array with the numbers, adding more of the numbers you want more of and choose a random over the length of the array.
Try this:
public int weightedRandom() {
int rand = Math.floor(Math.random() * 5 + 1) // From 1 to 5
if (Math.random() >= 0.6) {
++rand;
}
return rand;
}

Categories

Resources