This is a Java problem I am having, I am somewhat new to Java.
I have to:
generate a random fraction
keep generating until the sum is greater than 1
then I have to display how many numbers I generated and which ones
Here is what I have so far in my function:
public static double calcAvg(int numOfTimes){
double sumOfRand = 0 ;
int numOffractions = 0;
double avg = 0;
double numOfAvg[] = new double[numOfTimes]; // number of rows
double randNums[] = new double[] {.1,.2,.3,.4,.5,.6,.7,.8,.9};
Random rand = new Random();
for(int i = 0; sumOfRand <= 1; i++){
int randNum = rand.nextInt(randNums.length); //gets a number from array randomly
//System.out.println(randNums[randNum]);
sumOfRand += randNums[randNum]; //adds it to the sum
numOffractions++; //counts # of avgnums needed for > 1
numOfAvg[i] = numOffractions;
}
avg = (numOfAvg[0] + numOfAvg[1] + numOfAvg[2]) /(numOfTimes);
return avg;
}
I keep getting an error on: numOfAvg[i] = numOffractions;
and I can't seem to add the fractions to the sum until they pass 1.
Well, your code is not related to your requirements..
You need something like this:
public static double calcAvg(){//What is the numOfTimes variable?!
double randNums[] = new double[] {.1,.2,.3,.4,.5,.6,.7,.8,.9}; //These can be define outside
Random rand = new Random();
List<Double> numbers = new LinkedList<Double>(); // This will have the list of numbers
double sum = 0;
while(sum<1){
int randNum = rand.nextInt(randNums.length);
numbers.add(randNums[randNum]);
sum+=randNums[randNum];
}
for(Double d:numbers){//Print the numbers
System.out.println(d);
}
System.out.println("Average: ", sum/numbers.size());
}
Hope you get array index out of bound when you call the function calcAvg with 1,2,3.
within the calAvg method you are using the numOfTimes as the array length and accessing numofAvg variable beyond the length range.
The logic needs to be revisited based on your requirement.
Related
thanks in advance for any help I'm in an intro to java class and our home work was to generate 10 random numbers between 1&50 which I got and then average the generated numbers. I can't figure out how to average them here's what I have. Is there a way to store each random number as a variable?
public class randomNumberGen
{
public static void main(String [] args)
{
Random r=new Random();
for (int i=1;i<=10;i++){
System.out.println(r.nextInt(50));
System.out.println();
int average = (i/4);
System.out.println("your average is"+average);
}
}
}
use streams with java 8
final int numberOfRandom = 10;
final int min = 0;
final int max = 50;
final Random random = new Random();
System.out.println("The ave is: "+random.ints(min, max).limit(numberOfRandom).average());
First of all you have to replace "r.nextInt(50)" for "r.nextInt(50) + 1" because r.nextInt(n) returns a number between 0 (inclusive) and n (exclusive). Then, you know that an average is just a sum of n values divided by n. What you can do is just declare a "total" variable initialized to 0 before the loop. On each iteration you add to this variable the random value generated by r.nextInt(50). After the loop you can just divide the total by 10 so you get the average.
PS: it's a good practice to don't use "magic numbers", so it would be perfect (and luckily your teacher will have it in count) if you declare a constant for the number of iterations and then use it both in the loop condition and in the average calculation. Like this, if you have to make it for 100 numbers you only have to change the constant value from 10 to 100 instead of replacing two 10's por two 100's. Also this gives you the chance to give semantic value to these numbers, because now they will be "AMOUNT_OF_NUMBERS = 10" instead of just "10".
Like every average, it's sum of elements / amount of elements. So let's apply it here:
import java.util.Random;
public class randomNumberGen
{
public static void main(String [] args)
{
Random r=new Random();
double sum = 0; // is double so to prevent int division later on
int amount = 10;
int upperBound = 50;
for (int i = 0; i < amount; i++){
int next = r.nextInt(upperBound) + 1; // creates a random int in [1,50]
System.out.println(next);
sum += next; // accumulate sum of all random numbers
}
System.out.println("Your average is: " + (sum/amount));
}
}
Store variables outside of the loop to store both the total amount of numbers generated as well as the sum of those numbers. After the loop completes, divide the sum by the total amount of numbers.
public static void main(String [] args)
{
Random r=new Random();
double sum = 0;
int totalNums;
for (totalNums=1;totalNums<=10;totalNums++){
int randomNum = r.nextInt(50);
sum += randomNum;
System.out.println(randomNum);
}
double average = sum/totalNums;
System.out.println("your average is: "+average);
}
Average = Sum of numbers / amount of numbers
int sum = 0;
for (int i=1;i<=10;i++){
sum += r.nextInt(50) +1; //nextInt 50 produces value 0 to 49 so you add 1 to get 1 to 50 OR as suggested in the comments sum/10d
}
System.out.println("Average is: " + sum/10) // If you want the result in double (with decimals) just write sum*1.0/10
You could also do the same with a while loop.
int i = 0;
int sum = 0;
while(i < 10){
sum += r.nextInt(50) +1;
i++;
}
System.out.println("Average is: " + sum*1.0/i);
Or even shorter with lambda expressions: (/java 8 streams)
OptionalDouble average = IntStream.range(1, 10).map(x-> x = r.nextInt(50) +1).average();
System.out.println("Average is "+ average.getAsDouble());
.map(x-> x = r.nextInt(50) +1) // maps (changes) each value from 1 to 10 to a random number between 1 and 50
.average(); // calculates the average.
Simply create a variable sum starting at zero that you increment at each iteration. After the loop, simply divide by the number of elements..
Average means you should add everything up and devide it by the number of elements (50).
import java.util.Random;
class Homework {
public static final Random RANDOM = Random(); // never regenerate randoms
public static void main(String args[]) {
final int N = 50;
int sum = 0;
for (int i = 0; i < N; ++i) {
sum += RANDOM.nextInt(50)+1;
}
System.out.println("Avg: "+ sum / (float) N);
}
}
This should do the trick. Try to learn from it not just C+P.
Ps: Friggin annoying to write code on a phone.
I found answers on how to generate random numbers but nowhere how to generate all the numbers in the range without duplication in Java. Please share if you have a solution. Below is what I did but it simply generates randomly the numbers. I need to print out all numbers in the range without duplication!
package com.company;
import java.util.*;
public class RandomizeNumbers {
public static void main(String[] args) {
//Create Scanner
Scanner userInput = new Scanner(System.in);
//Ask for numbers N and M
System.out.println("Please enter two numbers and the program will randomize the numbers between them. " +
"The first number N must be bigger or equal to the second number M");
System.out.println("Please enter the first number N");
int n = userInput.nextInt();
System.out.println("Please enter the second number M");
int m = userInput.nextInt();
Random randomGenerator = new Random();
int difference = n - m;
//Randomize the numbers
if (m<=n){
for(int i = 0; i<= difference; i++ ) {
int randomInt = randomGenerator.nextInt(n - m + 1) + m;
System.out.println(randomInt);
}
}
else{
System.out.println("Please enter M less or equal to N");
}
}
}
What you need maybe generating a random permutation, pls see this link How to generate a random permutation in Java?
You can store generated number in a array.then after generate the next number check is there this number in array or no.
There are many ways to achieve this, lets suppose you want 50 numbers between A and B, then use a java.util.Set, since this collection does "ignore" duplicated values: following snippet describe it better:
Set<Integer> setA = new HashSet<Integer>();
Random r = new Random(System.currentTimeMillis());
int low = 10;
int high = 100;
int rnd = r.nextInt(high - low + 1) + low;
int maxCount = 50;
while (setA.size() < maxCount ) { //<--how many random numbers do you need?
rnd = r.nextInt(high - low + 1) + low;
setA.add(rnd);
}
and be careful, not to get in an infinite loop.
(there are only "B-A" possible integer options between A and B, so MaxCount<= B-A)
What I suggest you to do is to create a List and then shuffle it.
ArrayList<Integer> list = new ArrayList();
int high = 20;
int low = 10;
for(int i = low; i <= high; ++i)
list.add(i);
Collections.shuffle(list);
And then create a function to get a random Unique number each time.
static int index = 0;
public int roll(ArrayList<Integer> list)
{
return list.get(index ++);
}
You can put all the numbers between n & m into a list and then use Collections.shuffle(list) to make the numbers ordered randomly in the list.
if (difference > 0) {
List<Integer> integers = new ArrayList<>();
for (int i = 0; i <= difference; ++i) {
integers.add(m + i);
}
Collections.shuffle(integers);
for (Integer randNum : integers) {
System.out.print(randNum + "\t");
}
System.out.println();
} else {
System.out.println("Please enter M less or equal to N");
}
I need to create an array of random integers which a sum of them is 1000000 and an average of these numbers is 3. The numbers in the array could be duplicated and the length of the array could be any number.
I am able to find the array of random integers which the sum of them is 1000000.
ArrayList<Integer> array = new ArrayList<Integer>();
int a = 1000000;
Random rn = new Random();
while (a >= 1)
{
int answer = rn.nextInt(a) + 1;
array.add(answer);
a -= answer;
}
However, I don't know how to find the random numbers with average of 3.
that's mathematically not possible:
you are looking for n values, sum of which makes 1000000, and the average of them is 3, which is 1000000/n. since n can only take integer values it is not possible.
If they are constrained to an average and random, they must be constrained to a value range. A range of 1 to 5 (median is 3) seems reasonable. Also reasonable is a smooth distribution, which gives a known total and average.
This simple code will do all that:
List<Integer> numbers = new ArrayList<>(333334); // 1000000 / 3
// one instance of 5 must be omitted to make total exactly 1000000
numbers.addAll(Arrays.asList(1, 2, 3, 4));
for (int i = 0; i < 333330; i++)
numbers.add((i % 5) + 1); // add 1,2,3,4,5,1,2,3,4,5,etc
Collections.shuffle(numbers);
// Check sum is correct
numbers.stream().reduce((a,b) -> a + b).ifPresent(System.out::println);
Output:
1000000
Note that it is mathematically impossible for the average to be exactly 3 when the total is 1000000 (because 1000000/3 has a remainder of 1/3), however this code gets pretty close:
1000000/333334 => 2.999994
I would transverse the list twice, and IF the integers at these two positions added together and divded by 2 == 3 then return, else, increment your integer.
As Göker Gümüş said it is mathematically impossible to have the average be exactly 3 and the sum be a million.
The average = sum / number of elements.
This means that number of elements = sum / average.
In this case it would need 1000000 / 3 = 333333.(3) elements. Since you can't have a third of an element with value 3 it means your average or your sum will need to be slightly off your target for it to match up.
The less notable needed difference would definitely be the average as it would only need to be a millionth of a unit off, i.e 3.000001 for you to be able to have 333333 elements summing to 1000000
I think that you need to write a simple average function like:
public double average(ArrayList<Integer> array){
long sum = 0;
int count = 0;
for (Integer item : array){
sum += item;
count++;
}
return sum/count;
}
Then use it in your code like:
ArrayList<Integer> array = new ArrayList<Integer>();
int a = 1000000;
Random rn = new Random();
boolean isDone = true;
while (a >= 1)
{
int answer = rn.nextInt(a) + 1;
array.add(answer);
a -= answer;
if (average(array) % 3 != 0){
isDone = false;
break;
}
}
The idea is each time we adding a new number to the array, we checking that the average can be divide with 3, if not, we getting out of the while loop.
To let us know if the algorithm went well, we need to check isDone variable at the end.
And the more efficient way is:
ArrayList<Integer> array = new ArrayList<Integer>();
int a = 1000000;
Random rn = new Random();
boolean isDone = true;
long sum = 0;
while (a >= 1)
{
int answer = rn.nextInt(a) + 1;
array.add(answer);
a -= answer;
sum += answer;
if ((sum/array.size()) % 3 != 0){
isDone = false;
break;
}
}
there are many answers to this question but lets say we want our random number to be 10 max (which we can change). I guess this would give a satisfactory answer.
import java.util.Random;
import java.util.ArrayList;
public class RandomSumAverage {
public static void main(String[] args) {
Random random = new Random();
ArrayList<Integer> arr = new ArrayList<Integer>();
double sum = 0;
double avg = 0;
int k = 1;
while (sum < 1000000) {
avg = sum / k;
if (avg < 3) {
int element = random.nextInt(10)+1;
sum += element;
arr.add(element);
k++;
} else {
int element = random.nextInt(3)+1;
sum += element;
arr.add(element);
k++;
}
}
System.out.println(arr);
System.out.println(sum);
System.out.println(avg);
}
}
I know for generating random number there is random function in java
for example
int randomNumber = ( int )( Math.random() * 9999 );
which will generate randomNumber from [0,9999] but it only returns one number by given range.
But I just want to know if there is any built in function or you can say native function which will generate more than one random numbers and it should also not match with each other
suppose from above example if i want to generate 4 number
it will return like 1,10,50,5544
Here you can see that there is a four random number and it is not matching with each other .
try something like this
Create an ArrayList and add your random number and while adding check if ArrayList already contains the number or not.
Eg:
ArrayList<Integer> numbers = new ArrayList<Integer>();
while (numbers.size()<=YOUR_MAX_SIZE)
{
int randomInteger = ( int )( Math.random() * 9999 );
if (!numbers.contains(randomInteger)) {
{
numbers.add(randomInteger);
}
}
Another technique (besides the one shown by #MichaelShrestha) is to put the entire range of numbers in a collection, then shuffle it and take the numbers in the shuffled order.
It has the advantage that whereas the other method might spin though many (many) numbers to find a non-duplicate random value, this will only have to be run once. OTOH, for small collections of numbers, #Michael's technique might be faster.
Try this, as proposed here:
int min = 1;
int max = 10000;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
EDIT:
With no duplicates:
Random rng = new Random(); // Ideally just create one instance globally
int min = 1;
int max = 10000;
Set<Integer> generated = new LinkedHashSet<Integer>();
while (generated.size() < numbersNeeded)
{
Integer next = r.nextInt(max - min + 1) + min;
generated.add(next);
}
LINK
There is not built in api. but you can use something like this
public static int[] getRandoms(int amount, int range){
int[] result = new int[amount];
ArrayList tempList = new ArrayList();
for(int i = 0; i< range; i++)
tempList.add(i);
Collections.shuffle(tempList);
for(int i = 0; i< amount; i++)
result[i] = (int)tempList.get(i);
return result;
}
Here amount is the number of randome numbers you want and the range is the maximum range of random number
ArrayList<Integer> rnumbers = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
int randomNumber = (int) (Math.random() * 9999);
while (rnumbers.contains(randomNumber)) {
randomNumber = (int) (Math.random() * 9999);
}
rnumbers.add(randomNumber);
}
As HashSet contains unique object... You can have the unique random numbers.
Set uniqueRandomNumber = new HashSet();
int i = 5 //number Of Random Number Required;
for(int = 0; i<5; 1++){
int randomNumber = ( int )( Math.random() * 9999 );
uniqueRandomNumber.add(randomNumber);
}
Quick question, I found answers close to this but nothing that helps me. I want it to print out a percentage at the end of the code that has 4 numbers after the decimal point, and of course, using an int work work. But using floats gives me an error.
This code:
import java.util.Scanner;
public class HW2johnson_pp4 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("How many numbers will you enter?\n");
float[] numbers = new float[keyboard.nextFloat()];
System.out.printf("Enter " + numbers.length + " integers, one per line:\n");
for (int i = 0; i <= numbers.length - 1; i++) {
numbers[i] = keyboard.nextInt();
}
float sum = 0;
for (int i = 0; i <= numbers.length - 1; i++) {
sum += numbers[i];
}
System.out.printf("The sum is " + sum + "\n");
System.out.printf("The numbers are:\n");
for (int i = 0; i <= numbers.length - 1; i++) {
float perc = (numbers[i] / sum);
float perct = (perc * 100);
System.out.printf(numbers[i] + " which is " + perct + "%% of the sum.\n");
}
}
}
Gives the error:
HW2johnson_pp4.java:8: possible loss of precision
found : float
required: int
float[] numbers = new float[keyboard.nextFloat()];
You can't create an array of floats whose length is a floating-point value. That is to say, you can't have an array with 2.7 elements.
So the float within the length parameter is getting rounded, causing a loss of precision.
You wanted keyboard.nextInt() there on line 8, and keyboard.nextFloat() below on line 13.
You cannot initialize an array with floating point values.
float[] a = new float[4]
And not
float[] a = new float[4.0]
So, The problem is here:
float[] numbers = new float[keyboard.nextFloat()];
Use keyboard.nextInt() instead.
You're using keyboard.nextFloat(), which is a float, as the length of the array numbers. The length of an array has to be an int.
Thats because in line 8 you are making a new array, and passing a float as the length. Since all arrays require an integer as an array length, it will convert the float to an integer, and gives an error. You want to pass in an integer value.