Java Random Utility Generating Too Many 0's And Static Numbers - java

The line birthdays[j] = rnd.nextInt(365); seems to generate extra 0's in the int[] birthdays array. It also seems to add an EXTRA 0 into the array and generate static values depending on how many simulations I run and how many birthdays I generate. For instance, if I do 5 simulations and enter a 3 for the number of people in each simulation's "birthday pool" I always get an array of [0, 0, 289, 362].
Any help understanding the problem would be greatly appreciated.
public static void main(String[] args) {
System.out.println("Welcome to the birthday problem Simulator\n");
String userAnswer="";
Scanner stdIn = new Scanner(System.in);
do {
int [] userInput = promptAndRead(stdIn);
double probability = compute(userInput[0], userInput[1]);
// Print results
System.out.println("For a group of " + userInput[1] + " people, the probability");
System.out.print("that two people have the same birthday is\n");
System.out.println(probability);
System.out.print("\nDo you want to run another set of simulations(y/n)? :");
//eat or skip empty line
stdIn.nextLine();
userAnswer = stdIn.nextLine();
} while (userAnswer.equals("y"));
System.out.println("Goodbye!");
stdIn.close();
}
// Prompt user to provide the number of simulations and number of people and return them as an array
public static int[] promptAndRead(Scanner stdIn) {
int numberOfSimulations = 0;
while(numberOfSimulations < 1 || numberOfSimulations > 50000) {
System.out.println("Please Enter the number of simulations to do. (1 - 50000) ");
numberOfSimulations = stdIn.nextInt();
}
int sizeOfGroup = 0;
while(sizeOfGroup < 2 || sizeOfGroup > 365) {
System.out.println("Please Enter the size of the group of people. (2 - 365) ");
sizeOfGroup = stdIn.nextInt();
}
int[] simulationVariables = {numberOfSimulations, sizeOfGroup};
return simulationVariables;
}
// This is the method that actually does the calculations.
public static double compute(int numOfSims, int numOfPeeps) {
double numberOfSims = 0.0;
double simsWithCollisions = 0.0;
int matchingBirthdays = 0;
int[] birthdays = new int[numOfPeeps + 1];
int randomSeed = 0;
for(int i = 0; i < numOfSims; i++)
{
randomSeed++;
Random rnd = new Random(randomSeed);
birthdays = new int[numOfPeeps + 1];
matchingBirthdays = 0;
for(int j = 0; j < numOfPeeps; j++) {
birthdays[j] = rnd.nextInt(365);
Arrays.sort(birthdays);
}
for(int k = 0; k < numOfPeeps; k++) {
if(birthdays[k] == birthdays[k+1]) {
matchingBirthdays++;
}
}
if(matchingBirthdays > 0) {
simsWithCollisions = simsWithCollisions + 1;
}
}
numberOfSims = numOfSims;
double chance = (simsWithCollisions / numberOfSims);
return chance;
}
}

The line "birthdays[j] = rnd.nextInt(365);" seems to generate extra 0's in the int[] birthdays array.
Well, it doesn't. The array elements where zero to start with.
What that statement actually does is to generate a single random number (from 0 to 364) and assign it to one element of the array; i.e. the jth element. That is not what is required for your problem.
Now, we could fix your code for you, but that defeats the purpose of your homework. Instead I will give you a HINT:
The birthdays array is supposed to contain a COUNT of the number of people with a birthday on each day of the year. You have to COUNT them. One at a time.
Think about it ...

int arrays are by default initialized to 0 unless explicitly specified. Please see this Oracle tutorial about Arrays.

I found the problem myself. The issue was that having the "Arrays.sort(birthdays);" statement inside of a loop. That generated extra 0's.

Related

Java lottery program, having trouble comparing outputs

I have to do an 'instant lottery' program in my first computer science class. All semester my professor has read verbatim from the book, so now I am a little lost, truthfully. I know how to do most of it, but am just having trouble figuring out array sort and how to compare user input and the random number output. My professor refuses to answer questions about take home assignments and has banned the use of anything except: arrays, loops and math.random- so no sets or anything more complex that could help. I've seen other programs that compile, but all with sets.
I have the code for user input of the lottery numbers and to generate the output of the random numbers. I can most likely also figure out how to print the payout with if/else. I just need to know how to get the program to compare the numbers an figure out if the user is a "winner" or not.
import java.util.Scanner;
public class TheLottery {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //user input of their lottery numbers
System.out.print("Enter number 1: ");
int num1 = keyboard.nextInt();
System.out.print("Enter number 2: ");
int num2 = keyboard.nextInt();
System.out.print("Enter number 3: ");
int num3 = keyboard.nextInt();
System.out.print("Enter number 4: ");
int num4 = keyboard.nextInt();
System.out.print("Enter number 5: ");
int num5 = keyboard.nextInt();
System.out.print("Enter number 6: ");
int num6 = keyboard.nextInt();
}
int[] lottery = new int[6];
int randomNum;
{
for (int i = 0; i < 6; i++) {
randomNum = (int) (Math.random() * 50); // Random number created here.
for (int x = 0; x < i; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
x = -1; // restart the loop
}
}
lottery[i] = randomNum;
}
for (int i = 0; i < lottery.length; i++)
System.out.print(lottery[i] + " "); //print random numbers
}
}
the final program should have the user enter 6 numbers, the program compare the numbers for matches, figure out if the user is a 'winner', show the prize and an added thing is show how much they spent (each 'ticket' is $1) vs how much they won. So far all that outputs is the scanner and random numbers
It looks like you obtained six numbers then didn't use them. Your lottery array is automatically initialized to zero. I think you're trying to compare an array with inputs to a random array, so you need a loop to put your entered values into. After you do that, initialize your random array, then just compare the arrays.
public static void main(System[] args) {
Scanner in = new Scanner(System.in);
int[] lottery = new int[6];
System.out.println("Enter " + lottery.length + " numbers: ");
for (int i = 0; i < lottery.length; i++) {
lottery[i] = in.nextInt();
}
The specific question has to do with how to do the comparison and figuring a "winner". It isn't clear what makes the definition of "winner".
Based upon my comment, and as shown in the answer by #szoore, I would use an array to collect the input. I'd use a method to collect (since one can change to use a different method for the selections, which makes testing easier).
/**
* Obtain the specified number of entries from the user
*/
public static int[] getUserSelections(final int numSelections)
{
Scanner in = new Scanner(System.in);
// read N entries from the user
int[] nums = new int[numSelections];
// NOTE: no error processing in this loop; should be refined
// bad numbers (e.g., negative, too large), duplicate entries, etc.
// need to be removed
for (int i = 0; i < numSelections; ++i) {
System.out.print("Enter number " + (i + 1) + ": ");
nums[i] = in.nextInt();
}
return nums;
}
The OP has a basic generation for the lottery numbers, but again I'd use a method. This has a slight refinement to the duplicate check, by using a method, and also allows the same duplicate check method to be later used for checking matches:
public static int[] getLotteryNumbers(final int numSelections)
{
// the largest number to be selected; all numbers between
// 1 and maxNum (inclusive) will have equal(-ish) probability
// of being generated
final int maxNum = 50;
int[] lottery = new int[numSelections];
Random rnd = new Random();
// make N random selections, and ensure we don't have duplicates
for (int i = 0; i < numSelections; ++i) {
boolean generate = true;
while (generate) {
int sel = rnd.nextInt(maxNum) + 1;
generate = numberInArray(sel, lottery);
if (! generate) {
lottery[i] = sel;
}
}
}
return lottery;
}
/**
* Returns true if the specific queryNum is found in the pastSelections
* Could be slightly optimized by passing how many selections have
* already been made
*/
public static boolean numberInArray(int queryNum, int[] pastSelections)
{
// look at each element and see if already there; exit via return
// if so
for (int i = 0; i < pastSelections.length; ++i) {
if (pastSelections[i] == queryNum) {
return true;
}
}
return false;
}
The use of the method 'numberInArray' then allows for fairly easy check on how many numbers match:
// see how many match
int matches = 0;
// see if the user entry exists in the lottery; if so, we
// have a match
for (int i = 0; i < userEntries.length; ++i) {
if (numberInArray(userEntries[i], lottery)) {
++matches;
}
}
System.out.printf("Found %2d matches%n", matches);
Determining payouts, etc. is straight forwarding using if/else or (perhaps better) a switch based on the number of matches.
Also, the entries and lottery selections should probably be sorted. It isn't clear if the built-in sort may be used or not. Write the sort method as appropriate:
/**
* Sorts the array; implement sorting as needed
*/
public static void sort(int[] arr)
{
Arrays.sort(arr);
}
/*
* outputs the array if one cannot use Arrays.toString(arr)
*/
public static void outputArray(int[] arr)
{
for (int i = 0; i < arr.length; ++i) {
System.out.printf("%2d ", arr[i]);
}
System.out.println();
}
Sample main:
public static void main(String[] args)
{
// how many options for the lottery; here it is 6
final int numEntries = 6;
// this method obtains from user
int[] userEntries;
userEntries = getUserSelections(numEntries);
sort(userEntries);
// display User selections
outputArray(userEntries);
int[] lottery = getLotteryNumbers(numEntries);
sort(lottery);
// display lottery numbers
outputArray(lottery);
// see how many match
int matches = 0;
// see if the user entry exists in the lottery; if so, we
// have a match
for (int i = 0; i < userEntries.length; ++i) {
if (numberInArray(userEntries[i], lottery)) {
++matches;
}
}
System.out.printf("Found %2d matches%n", matches);
//
// TODO: calculate winnings based upon the number of matches
//
}

Moving Average Using User-Input Array

I need to write a program that calculates a moving average by a user inputted array. The first element of the array is the window size, and the input is terminated by a 0. The output values are printed with two digits after the decimal point.
Example input: 3 2 4 7 7 8 11 12 0
Corresponding Output: 4.33 6.00 7.33 8.67 10.33
(4.33 is average of 2,4,7 and 6 is average of 4,7,7 etc.)
Here's my code so far:
package movingaverage;
import java.util.Scanner;
public class MovingAverage {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
avg[0] = sum / 5;
int j = 1;
for (int i = 5; i < arr.length; i++) {
sum = sum + arr[i] - arr[i - 5];
avg[j++] = sum / 5;
}
}
}
I think I have the loop right, but I'm not sure how to get the array to end at 0.
This is a possible solution.
public class Test
{
private static final Scanner SCANNER;
static {
SCANNER = new Scanner(System.in);
}
public static final void main(final String... args) {
final String[] numbers = SCANNER.nextLine().trim().split(" ");
final int consideredElements = Integer.parseInt(numbers[0]);
float sum = 0;
int value = 0;
for (int i = 1; i < numbers.length; i++) {
sum = 0;
for (int k = 0; k < consideredElements; k++) {
value = Integer.parseInt(numbers[i + k]);
if (value == 0) {
return;
}
sum += value;
}
System.out.println(new BigDecimal(sum / consideredElements).setScale(2, RoundingMode.HALF_EVEN));
}
}
}
First, you are using 5 in a couple of places in your program, I see no justification for that. Could it be that your expectation of user input lead you to put 5 where the number you really should use, depends on user input? Maybe you should use the window size instead? I’m guessing a bit here.
Next, as #lppEdd pointed out, you are not reading the numbers from your input — only the window size.
Next, you are declaring your array of size n, which I believe was your window size, not your array size. I believe the real solution to this problem is using better and more explanatory variable names.
Your code does not compile since you have not declared the array avg that you try to store your moving average into.
Fifth, when you want your average as a double, you need to convert to double before dividing (this is a classic pitfall that has already generated many questions on Stack Overflow).
I hope this gets you a couple of steps further.

How to generate all numbers randomly between two given integers without duplication in Java?

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

How do you put a string inside a 2d Array

I am trying to write a polling program that takes five issues and puts the issue on the row of a 2d Array. Also how would I make the program count how many times a person rated the issue. For example if five people gave rating of five how would I write the program to count the rating and put it on the 2d Array.
These are the instructions:
Write a simple polling program that:
Allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important);
Pick five causes that are important to you (e.g., political issues, global environmental issues). Use a one-
dimensional array topics (of type String) to store the five causes;
To summarize the survey responses, use a 5-row, 10-column two-dimensional array responses (of type int):
Each row corresponding to an element in the topics array.
When the program runs, it should ask the user to rate each issue. People in the range of (5, 13) have respond to the survey. Then have the program display a summary of the results, including:
a) A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic.
b) To the right of each row, show the average of the ratings for that issue.
c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.
This is my code:
import java.util.Arrays;
import java.util.*;
public class Polling {
/**
* #param args the command line arguments
*/
public static String[] issues=new String[20];
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
issues[0]="Global Warming";
issues[1]="Earth Quakes";
issues[2]="Stopping war";
issues [3]="Equal Rights";
issues[4]="Curing Cancer";
int[][] polling =new int[5][10];
Random rand=new Random();
int random=rand.nextInt(9)+5;
int poll=0;
String polling2=Arrays.toString(polling);
for(int i=1;i<random;i++){
System.out.println("Person"+i);
System.out.println("Rate these issues from 1-10");
System.out.println(issues[0]);
int zero=console.nextInt();
System.out.println(issues[1]);
int one=console.nextInt();
System.out.println(issues[2]);
int two=console.nextInt();
System.out.println(issues[3]);
int three=console.nextInt();
System.out.println(issues[4]);
int four=console.nextInt();
}
System.out.println();
Something like this?:
public static final String[] ISSUES = {
"Global Warming",
"Earth Quakes",
"Stopping war",
"Equal Rights",
"Curing Cancer",};
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random rand = new Random();
int pollings = rand.nextInt(9) + 5;
int [][] rates= new int[pollings][ISSUES.length];
for (int i = 0; i < pollings; i++) {
System.out.println("Person" + i);
System.out.println("Rate these issues from 1-10");
for (int j = 0; j < ISSUES.length; j++) {
System.out.println(ISSUES[j]);
rates[i][j] = console.nextInt();
}
}
// ADDED
int minRating = Integer.MAX_VALUE;
int maxRating = Integer.MIN_VALUE;
int minRatingIndex = -1;
int maxRatingIndex = -1;
for (int i = 0; i < ISSUES.length; i++) {
System.out.print(ISSUES[i]+":");
int rating = 0;
for (int j = 0; j < pollings; j++) {
System.out.print("\t"+rates[j][i]);
rating += rates[j][i];
}
double average = ((double)rating)/pollings;
System.out.println("\tavr: "+average);
if (rating < minRating ){
minRating = rating;
minRatingIndex = i;
}
if (rating > maxRating ){
maxRating = rating;
maxRatingIndex = i;
}
}
System.out.println("Max points:\t"+ISSUES[maxRatingIndex]+":\t"+maxRating+" points");
System.out.println("Min points:\t"+ISSUES[minRatingIndex]+":\t"+minRating+" points");
System.out.println();
}

Finding out the frequency of unique numbers

I am trying to solve a problem in Java as part of my assignment. The problem is as below:
The user enters ten numbers one by one upon prompting by the screen. The screen then assigns all the distinct value to an array and a similar array to hold the frequency of how many times those numbers have appeared.
I have done the below work, but seems I am stuck somewhere in assigning the frequencies and distinct values to the arrays:
import java.util.*;
public class JavaApplication10
{
public static void main(String[] args)
{
int [] numbers = new int [10];
int [] count = new int[10];
int [] distinct = new int[10];
for (int k=0;k<10;k++)
{
count[k]=0;
distinct[k]=0;
}
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter number 0: ");
numbers[0]=input.nextInt();
count[0]=1;
distinct[0]=numbers[0];
int j=0;
for (int i = 1;i<10;i++)
{
System.out.print("Enter number "+i+": ");
numbers[i]=input.nextInt();
while(j<i)
{
if (distinct[j]==numbers[i])
count[j]=count[j]+1;
else
distinct[j+1]=numbers[i];
j++;
}
}
for (int k=0;k<10;k++)
{
System.out.println(distinct[k]+ " "+count[k]);
}
}
}
I know that it is not fair to ask someone to help me solve the problem. But any kind of hint will be helpful.
Thank you
are the numbers limited to 0-9? If so, I would simple do the assignment.
(please note you will assign the input to a variable called "input"):
numbers[0]=input;
count[input]++;
Also you can start your for loop in "0" to avoid the assignment prior to the for loop.
Just a hint.
Hope this helps!
the ideal data structure would be a HashMap
Steps:
1) initialize an array to store the numbers and for each input
2) check if a hashmap entry with key as the entered number already exists
3) if exists simply increase its count
4) else create new entry with key as the number and count as 1
so at the end your frequencies would be calculated
if you are forced to use 2 arrays
1) initialize two arrays
2) for each input loop the number array and check whether that number is already in the array
3) if so take the array index and increment the value of the frequency array with the same index
4) if not freq[index] = 1
A proper way of doing that would be:
public Map<Integer, Integer> getFrequencies(Iterable<Integer> numbers) {
Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>();
for(Integer number : numbers) {
if (frequencies.get(number) == null) {
frequencies.put(number, 0);
}
frequencies.put(number, frequencies.get(number) + 1);
}
return frequencies;
}
It returns a map number -> frequency.
Arrays are not a way to go in Java, they should be avoided whenever possible. See Effective Java, Item 25: Prefer lists to arrays.
I removed the Scanner object to write the code faster, just replace it with your code above and it should work.
int[] numbers = { 1, 2, 2, 2, 3, 3, 3, 1, 1, 2 };
int[] count = new int[10];
int[] distinct = new int[10];
count[0] = 1;
distinct[0] = numbers[0];
int disPos = 1; //Current possition in the distinct array
boolean valueInarray = false;
for (int i = 1; i < 10; i++) {
valueInarray = false;
for (int d = 0; d < i; d++) {
if (numbers[i] == distinct[d]) {
count[d] = count[d] + 1;
valueInarray = true;
break;
}
}
if (!valueInarray) {
distinct[disPos] = numbers[i];
count[disPos] = 1;
disPos++;
}
}
If you ABSOLUTELY HAVE TO use arrays.. here is a way to do it…
import java.util.Scanner;
import java.util.Arrays;
public class JavaApplication10
{
public static void main(String[] args)
{
int [] numbers = new int [10];
int [] count = new int[10];
int [] distinct = new int[10];
int [] distinct1 = new int[1];
int distinctCount = 0;
boolean found = false;
Scanner input = new Scanner(System.in);
for (int i=0; i<10; i++) {
found = false;
System.out.print("Enter number " + i);
numbers[i]=input.nextInt(); //Add input to numbers array
for (int j=0; j<=distinctCount; j++)
{
if (distinct1[j] == numbers[i]){ // check to see if the number is already in the distinct array
count[j] = count[j] + 1; // Increase count by 1
found = true;
break;
}
}
if (!found) {
distinct[distinctCount] = numbers[i];
count[distinctCount] = 1;
distinctCount++;
distinct1 = Arrays.copyOf(distinct, distinctCount+1);
}
}
for (int j=0; j<distinctCount; j++)
System.out.println("The number " + distinct1[j] + " occurs " + count[j] + " times" );
}
}
I think this is what you need, correct me if I'm wrong...
import java.util.HashMap;
import java.util.Scanner;
public class JavaApplication10 {
public static void main(String[] args) {
// Initializing variables
int[] numbers = new int[10];
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
Scanner input = new Scanner(System.in);
// Getting the 10 inputs
for(int x=0; x<10; x++) {
// Asking for input
System.out.println("Enter number "+x+":");
numbers[x]=input.nextInt();
// If the table contains the number, add 1
// Otherwise: set value to 1
if(table.containsKey(numbers[x]))
table.put(numbers[x], table.get(numbers[x])+1);
else
table.put(numbers[x],1);
}
// Closing the reader
input.close();
// Get the highest and smallest number
int highest=0;
int smallest=0;
for(int i:table.keySet()) {
if(i>highest)
highest=i;
if(i<smallest)
smallest=i;
}
// For every value between the smallest and the highest
for (int x=smallest; x<=highest; x++) {
// Check if the frequency > 0, else continue
if(table.get(x)==null)
continue;
// Output
System.out.println(x+" is "+table.get(x)+" times in \'frequence\'");
}
}
}
This also handles with negative numbers, unlike the other's codes. If you don't want to use HashMaps let me know so I can create something with arrays.
Let me know if it (doesn't) works!
Happy coding (and good luck with your assignment) ;) -Charlie

Categories

Resources