For loop with error in code - java

UPDATE****
I have my program compiling and executing correctly but now I have faced another problem. I need to create variables that will count each time a certain random number is generated. For example count0 is supposed to record how many times the integer 0 is generated. This is what I have:
import java.util.Random;
public class L10{
public static void main(String[] args){
int total = 100;
Random randObj = new Random();
final int UPPER_BOUND = 10;
for (int i=0; i < total; i++){
int randomInt = randObj.nextInt(UPPER_BOUND);
System.out.print("\n" + randomInt);
int count0 = 0;
if(randomInt==0){
System.out.print(randomInt + count0);
}
int count1 = 1;
if(randomInt==1){
}
int count2 = 2;
int count3 = 3;
int count4 = 4;
int count5 = 5;
int count6 = 6;
int count7 = 7;
int count8 = 8;
int count9 = 9;
}
}
}
The output shows the random number, in this case zero, and prints a zero next to it. I'm not exactly sure how to write code that keeps track of how many times zero is generated. Any suggestions?

Try this:
import java.util.Random;
public class HelloWorld{
public static void main(String []args){
Random randObj = new Random();
final int UPPER_BOUND = 10;
int total = 100;
String star = "*";
for (int i=0; i < UPPER_BOUND; i++){
int randomInt = randObj.nextInt(total);
System.out.print(randomInt);
}
}
}
Modifications:
Random randObj = new Random();
int randomInt = randObj.nextInt(total);

I think your intention is to generate 100 random integers in the range 0-9, and count the frequency of each.
Using separate variables for each count is a poor idea. A better idea is to use a single array of size 10, whose index is the random number.
Modifying your code:
public static void main(String[] args){
final int TOTAL = 100, UPPER_BOUND = 10;
Random randObj = new Random();
int[] count = new int[UPPER_BOUND];
// collect frequencies
for (int i=0; i < TOTAL; i++)
count[randObj.nextInt(UPPER_BOUND)]++;
// report frequencies
for (int i=0; i < UPPER_BOUND; i++)
System.out.print(i + "'s frequency was " + count[i];
}

import java.util.Random;
public class L10{
public static void main(String[] args){
int total = 100;
int[] CountArray = new int[total]; //count numbers
Random randObj = new Random();
final int UPPER_BOUND = 10;
for (int i=0; i < total; i++){
int randomInt = randObj.nextInt(UPPER_BOUND);
System.out.print("\n" + randomInt);
switch(randomInt){
case(0):{
CountArray[0]++;
break;
}
case(1):{
CountArray[1]++;
break;
}
case(2):{
CountArray[2]++;
break;
}
case(3):{
CountArray[3]++;
break;
}
case(4):{
CountArray[4]++;
break;
}
case(5):{
CountArray[5]++;
break;
}
case(6):{
CountArray[6]++;
break;
}
case(7):{
CountArray[7]++;
break;
}
case(8):{
CountArray[8]++;
break;
}
case(9):{
CountArray[9]++;
break;
}
}
}
System.out.println("");
for(int j = 0;j<UPPER_BOUND;j++){
System.out.println("number of "+ j+" generated "+CountArray[j]);
}
}
}
This is the Code that I came up with. I used an array to count each element and used switch case to count them separately. so at the end of the for loop. I printed them. This way you can get the number of elements distinctively.

You are missing () after new Random.

Related

Number of times 3 is rolled

import java.util.Random;
public class RollingDice {
public static void main(String[] args){
int numSides = 6;
Random ranGen = new Random();
for (int i =1; i <= 20; i++){
if (ranGen.nextInt(numSides) == 3) {
System.out.println("A 3 has been rolled!");
}
}}}
this is my code so far. It prints the message every time the number 3 is rolled. I am new to coding, so please bear with me. What i want to do next is store the numbers of times 3 is rolled so when the loop exits, it displays the final count of the number of times 3 was actually rolled in that process. That making the end result be some number which represents the number of times the number 3 was rolled by the system.
Thanks!
-Sail
Define a count.
int count = 0;
Increase count each time you encounter a roll of 3. Inside of the loop, if you roll a 3:
count = count + 1;
Print count outside of the loop.
System.out.printf("A 3 was been rolled %d times.\n", count);
Random ranGen = new Random();
int numberOfThrees = 0;
for (int i =1; i <= 20; i++){
if (ranGen.nextInt(numSides) == 3) {
++numberOfThrees;
}
}
System.out.println(numberOfThrees);
Like this:
import java.util.Random;
public class RollingDice {
public static void main(String[] args){
int numSides = 6;
int threes = 0;
Random ranGen = new Random();
for (int i =1; i <= 20; i++) {
if (ranGen.nextInt(numSides) == 3) {
System.out.println("A 3 has been rolled!");
threes++
}
}
System.out.println("A 3 has been rolled " + threes + " times!");
}}
Actually, what you are recording is the number of 4's that have been rolled, since nextInt returns a number between 0 and 5.
You could simply have a counter
import java.util.Random;
public class RollingDice {
public static void main(String[] args){
int numSides = 6;
int cnt = 0; // <-- Declare a counter
Random ranGen = new Random();
for (int i =1; i <= 20; i++){
if (ranGen.nextInt(numSides) == 3) {
System.out.println("A 3 has been rolled!");
cnt++; // <-- increment counter
}
}
System.out.printf("A 3 has been rolled %d times!\n", cnt);
}

Java Simple Lottery Program

I tried to create a simple lottery program. Here is a problem: it still prints same numbers. For example I got 33 21 8 29 21 10 as output. Everytime when random number is generated, code checks if that number is already generated, then it creates a new random number but after that it doesn't check again. I couldn't find a way to do that.
public static void main(String[] args)
{
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[i] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * 50);//If random number is same, another number generated.
}
}
lottery[i] = randomNum;
}
for (int i = 0; i < lottery.length; i++)
System.out.print(lottery[i] + " ");
}
There are 2 problems with your code:
you check if lottery[i] and randomNum are the same, it should be lottery[x]
when you re-generate a random number, you don't check it against the first numbers in lottery.
Here is a corrected version:
public static void main(String[] args) {
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] + " ");
}
You are changing the random number while you are checking it. You need to pick one random number and check whether it is present or not.
BTW A shorter approach is to use a shuffle.
// give me all the number 1 to 50
List<Integer> list = IntStream.range(1, 51).boxed().collect(Collectors.toList());
// shuffle them.
Collections.shuffle(list);
// give me the first 6
System.out.println(list.subList(0, 6));
A simple solution, between the first (who could be very abstract for a not Java programmer) and the 2nd (not assuring the unicity of the number list).
Collection<Integer> liste = new ArrayList<Integer>();
for (int i = 0; i < 6; i++)
{
Boolean ap = false;
while (!ap)
{
Integer randomNumber = (int) (Math.random() * 50);
if (! liste.contains(randomNumber)){
liste.add(randomNumber);
ap = true;
}
}
}
for (Integer liste1 : liste) {
System.out.print(liste1+" ");
}
try this one, it creates 12 x (6 out of 45)
public static void main(String[] args) {
SecureRandom random = new SecureRandom();
for (int i = 0; i < 12; i++){
Integer[] tipp = new Integer[6];
int n = 0;
do {
int r = random.nextInt(45) + 1;
if (Arrays.asList(tipp).indexOf(r)<0){
tipp[n]= r;
n++;
}
} while (n<=5);
Arrays.sort(tipp);
System.out.println(Arrays.toString(tipp));
}
}
public static void main(String[] arg) {
int[] lottery = new int[6];
int randomNum;
c1:
for (int i = 0; i < 6; i++) {
randomNum = (int) (Math.random() * 50); // Random number created here.
if(randomNum == 0) {
continue c1;
}
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] + " ");
}
This is the object class for making a ticket, it will create ONE ticket with ascending values at which whatever parameters you choose. This program won't run until you have a main method that you call. Make sure to import TreeSet.
import java.util.TreeSet;
public class TicketMaker{
private int numbersPerTicket;
private int lowestNumber;
private int highestNumber;
TicketMaker(){
numbersPerTicket=0;
lowestNumber=0;
highestNumber=0;
}
TicketMaker(int numbersPerTicket,int lowestNumber,int highestNumber){
if(numbersPerTicket > 0 && lowestNumber >= 0 && highestNumber >= lowestNumber){
this.numbersPerTicket=numbersPerTicket;
this.lowestNumber=lowestNumber;
this.highestNumber=highestNumber;
}
}
public boolean printTicket(int numbersPerTicket,int lowestNumber,int highestNumber){
if(numbersPerTicket > 0 && lowestNumber >= 0 && highestNumber >= lowestNumber){
if(numbersPerTicket > highestNumber){
System.out.println("Error not in-bounds");
return false;
}
int rand;
int count=0;
System.out.println("[Ticket Printed]");
TreeSet<Integer> set = new TreeSet<>();
do{
rand = (int)(Math.random()*highestNumber)+lowestNumber;
set.add(rand);
count++;
}while(set.size() != numbersPerTicket);
System.out.println(set);
return true;
}
else{
System.out.println("Error not in-bounds");
return false;
}
}
public boolean isValidTicketData(int numbers,int lowest,int highest){
if(lowest != 1){
if(highest == numbers)
return false;
}
if(numbers <= highest){
if(numbers > 0 && lowest >= 0 && highest >= lowest)
return true;
}
return false;
}
}

Filling a int[] array

I must make a int array filled with 5000 numbers, and then take the value of each cell, which is a random number up to 1000, and multiply it by the square root of the cell index.
So far, my code is:
import java.util.*;
public class thousandArray{
public static void main (String args[]){
int numbers[] = new int[5000];
int r = 0 + (int)(Math.random()*1000);
double rt = numbers[r];
while(rt==numbers[r]){
r=0+(int)(Math.random()*1000);
double square = rt*Math.sqrt(numbers[r]);
System.out.println(square);
System.exit(0);
}
}
}
im pretty sure I did the code right, I cant figure out how to fill my array.
import java.util.*;
public class thousandArray{
public static void main (String args[]){
int numbers[] = new int[5000];
int r = 0 + (int)(Math.random()*1000);
double rt = numbers[r]; //you never assigned anything to numbers[r]
while(rt==numbers[r]){
r=0+(int)(Math.random()*1000);
double square = rt*Math.sqrt(numbers[r]);
System.out.println(square);
System.exit(0); //you exit the first time it loops
}
}
}
To fill an int array:
int[] numbers = new int[5000];
for (int i = 0; i < 5000; i++) {
numbers[i] = some_value;
}
To make the values be random, more or less as the problem states, you'd use:
numbers[i] = (int)(Math.random()*1000);
To multiply by the square root of the cell index:
numbers[i] = numbers[i] * (int)Math.sqrt(i);
Of course, that assumes that numbers should be an int array. It would make more sense for it to be a double array, in which case you'd remove the (int) casts.
public class ThousandArray {
public static void main (String args[]){
int MAX = 5000;
int numbers[] = new int[MAX];
for (int i = 0; i < MAX; i++) {
numbers[i] = (int)(Math.sqrt(i) * (Math.random()*1000));
}
}
}

Limiting duplicate random numbers

I just want to know how to limit to number of times a random number appears. I have generated random numbers of 1 to 10 and want to limit each number to appear 4 times.
myArray[i][j] = rand.nextInt(11);
for (int i=0; i < myArray.length; i++) {
for (int j=0; j < myArray[i].length; j++) {
myArray[i][j] = rand.nextInt(11);
System.out.print(" " + myArray[i][j]);
The code above creates the randoms numbers. Just want to limit them.
Since you are limited to 10 * 4 = 40 numbers you can use a list and randomize the index :
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i < 11; ++i) {
for (int j = 0; j < 4; ++j)
numbers.add(i);
}
And then when you assign a random number :
int i = rand.nextInt(numbers.size());
myArray[i][j] = numbers.get(i);
numbers.remove(i);
This assumes your two dimensional will not contain more then 40 numbers
My solution stores the result in arrayList:
public class Example {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final int range = 10;
int[] numbers = new int[range + 1];
int sum = 0;
final int noOfOccurances = 4;
final int size = range * noOfOccurances;
Arrays.fill(numbers, 0);
Random generator = new Random();
List<Integer> numbersArray = new ArrayList<>();
while (sum != size) {
int randomNumber = generator.nextInt(range) + 1;
if (numbers[randomNumber] != noOfOccurances) {
numbers[randomNumber]++;
sum++;
numbersArray.add(randomNumber);
}
}
System.out.println(numbersArray);
}
}
How about storing the count of the generated int's in an array, or Map, or anything?
Map<Integer, Integer> randomCounts = new HashMap<Integer, Integer>();
... your for loops
myArray[i][j] = rand.nextInt(11);
if (randomCounts.containsKey(myArray[i][j])) {
randomCounts.put(myArray[i][j],randomCounts.get(myArray[i][j])+1);
} else {
randomCounts.put(myArray[i][j],1);
}
And if you want to check them, just iterate through your map, and voilá. :)
You can make a method to check if the generated number exists more than 4 times in the array and create a new random number if it does. It should look like this:
import java.util.Random;
public class rndNumberGenerator {
public static void main (String[] args) {
int[][] myArray = new int[2][5];
Random rand = new Random();
int randomNumber;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
do {
randomNumber = rand.nextInt(11);
} while(overMax(myArray, randomNumber) == true);
myArray[i][j] = randomNumber;
System.out.print(" " + myArray[i][j]);
}
}
}
public static boolean overMax(int[][] array, int number) {
int max = 4;
int count = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
if (array[i][j] == number) {
count++;
}
}
}
if (count >= max)
return true;
else
return false;
}
}
Hope this helped you, if you have any other questions feel free to ask.
I take suggestion by pshemek (vote up): instead the ArrayList, I use the Set because it can't contain duplicate numbers and you have'nt to espicitate control.
An implementation: the copy{right, left} is of pshemek, I had only extended the idea:)
public class Example {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int[] numbers = new int[11];
int sum = 0;
final int range = 10;
final int noOfOccurances = 4;
Arrays.fill(numbers, 0);
Random generator = new Random();
Set<Integer> numbersArray = new TreeSet<Integer>();
while (sum != range * noOfOccurances) {
int randomNumber = generator.nextInt(range) + 1;
sum++;//correction for first comment
numbersArray.add(randomNumber); // randomNumber will never be twice: a Set cointains ever one and only one instance of an determinated element
}
System.out.println(numbersArray);
}
}//end class
You could write your own:
public static class CountedRandom {
// My rng.
Random rand = new Random();
// Keeps track of the counts so far.
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
// The limit I must apply.
final int limit;
public CountedRandom(int limit) {
this.limit = limit;
}
public int nextInt(int l) {
int r;
do {
// Keep getting a new number until we hit one that has'n been overused.
r = rand.nextInt(l);
} while (count(r) >= limit);
return r;
}
private int count(int r) {
// How many times have we seen this one so far.
Integer counted = counts.get(r);
if ( counted == null ) {
// Never!
counted = new Integer(0);
}
// Remember the new value.
counts.put(r, counted + 1);
// Returns 0 first time around.
return counted;
}
}
public void test() {
CountedRandom cr = new CountedRandom(4);
for ( int i = 0; i < 50; i++ ) {
System.out.print(cr.nextInt(4)+",");
}
System.out.println();
}
Note that this will hang if you ask for too may numbers in too small a range (as I have in my test).
Prints
2,0,1,2,1,1,3,3,0,3,0,2,2,0,1,3,
and then hangs.

need 2 int generators not generating same int

How would i make the 2 random int generators not generating the same integer? My sample class is below, assuming the program runs. Thank you for your help.
public class TestRandom{
int num;
public TestRandom(){
num = 0;
}
public TestRandom(int x){
num = x;
}
public String toString(){
String print;
if (num == 1)
print = "1";
else if(num == 2)
print = "2";
else
print = "---";
return print;
}
}
public class Test{
TestRandom[] s= new TestRandom[20];
Random ran = new Random();
public void setArray(){
for(int i=0; i<s.length; i++)
s[i] = new TestRandom();
for(int k=0; k<4; k++)
int RandomNum = ran.nextInt(s.length);
s[RandomNum] = new TestRandom(1);
for(int e=0; e<4; e++)
int RandomNum = ran.nextInt(s.length);
s.[RandomNum] = new TestRandom(2);
}
}
If you make two generators not generate the same number it's not random.
If you anyway want that functionality use a single instance and if the second number generated is the same as the previous one generate another number.
Note that randomness and uniqueness doesn't really go together.
try this
import java.util.Random;
public class TestRandom{
int num=0;
int num2=0;
Random ran = new Random();
num = ran.nextInt();
num2 = ran.nextInt();
System.out.print(num+" "+num2);
}
Add each generated int to a set.
Check each int generated to see if the int exists in the set.
If yes, generate a new int.
e.g.
public class Test{
TestRandom[] s= new TestRandom[20];
Random ran = new Random();
public void setArray(){
for(int i=0; i<s.length; i++)
s[i] = new TestRandom();
HashSet<Integer> usedInts = new HashSet<Integer>();
for(int k=0; k<4; k++) {
int RandomNum;
do {
RandomNum = ran.nextInt(s.length);
} while (!usedInts.add(RandomNum)); // loop until RandomNum not duplicated
s[RandomNum] = new TestRandom(1);
}
for(int e=0; e<4; e++) {
int RandomNum;
do {
RandomNum = ran.nextInt(s.length);
} while (!usedInts.add(RandomNum)); // loop until RandomNum not duplicated
s.[RandomNum] = new TestRandom(2);
}
}

Categories

Resources