I want to write a program that is randomizing 1000000 times and finally chooses the number from 1 - 45 that was randomly generated most of the times. The code I have written is this:
public class Randomizer {
public static int[][] numbers = new int[45][2];
public static int maxN = 0;
public static int finalChoice;
public static void main(String[] args){
for(int i = 0; i < 45; i++){
numbers[i][0] = i + 1;
numbers[i][1] = 0;
}
for(int i = 0; i < 1000000; i ++){
int rnd = (int)(Math.random() * 45 + 1);
for(int j = 0; j < 45; j++){
if(rnd == numbers [j] [0]){
numbers[j][1] ++ ;
break;
}
}
}
for(int i = 0; i < 45; i++){
if(maxN < numbers[i][1]){
finalChoice = numbers[i][0];
}
System.out.print(numbers[i][1]+" \t");
}
System.out.println("\nFinal Choice: "+finalChoice);
}
}
The problem is that it keeps printing 45. Where could the error be?
Your loop that generates the numbers and populates the array looks fine. The only problem is in the final loop, which searches for the highest occurring number.
You forgot to update maxN, so each iteration updates finalChoice (since maxN < numbers[i][1] is always true, assuming that each number from 1 to 45 was randomly chosen at least once) and it ends up being 45.
Fix :
for(int i = 0; i < 45; i++){
if(maxN < numbers[i][1]){
finalChoice = numbers[i][0];
maxN = numbers[i][1]; // add this
}
System.out.print(numbers[i][1]+" \t");
}
Try this:
Random r = new Random();
for (int i = 0; i <= 1000000; i++) {
int Low = 1;
int High = 45;
int R = r.nextInt(High - Low) + Low;
}
Related
I want to create an 8x8 array with JAVA, in which i want to have 1 eight times in random generated positions. All the other positions of the array are going to be 0. I am using this code but obviously it is not filling the array with 1 for a specific number of times.
public static void main(String[] args) {
int [][] arr = new int [8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
arr[i][j] = (int) (Math.random()*2);
}
}
for(int k = 0; k < 8; k++){
for(int l = 0; l < 8; l++){
System.out.print(arr[k][l] + " ");
}
System.out.println();
}
}
}
import java.util.Random;
int[][] array = new int[8][8];
Random r = new Random();
int a = r.nextInt(8);
int b = r.nextInt(8);
//insert 8 random 1's in the 8x8 matrix, no duplicates
//by default in Java the other places are
for(int i = 0; i < 8; i++){
while(array[a][b] == 1){
a = r.nextInt(8);
b = r.nextInt(8);
}
array[a][b] = 1;
}
I am trying to sort an array of random numbers without using the arrays.sort. I have the code, but it doesn't work. not sure where is the error. Any kind of help is appreciated.
import java.util.*;
public class Sort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("How many numbers do you want? ");
int howMany = in.nextInt();
int [] myArray = getRandomArray(howMany);
}
/* public static int bsearch(int[] arr, int key)
{
}*/
public static int[] getRandomArray(int howMany) {
int[] returnMe = new int[howMany]; // Assume size >= 0
Random rand = new Random();
for (int i = 0; i < howMany ; i++)
returnMe[i] = rand.nextInt(Integer.MAX_VALUE) + 1;
//System.out.print(returnMe[i] + " ");
for (int i = 1; i <= (howMany - 1); i++)
{
for (int j = 0; j < howMany - i -1; j++)
{
int tmp = 0;
if (returnMe[j] > returnMe[j+1])
{
tmp = returnMe[j];
returnMe[j] = returnMe[j + 1];
returnMe[j + 1] = tmp;
}
}
}
for ( int i = 0; i < howMany; i++)
System.out.println(returnMe[i] + " ");
return returnMe;
}
}
Your line
for (int j = 0; j < howMany - i -1; j++)
should be
for (int j = 0; j <= howMany - i -1; j++)
or alternatively, remove the "-1" and keep "<". Otherwise, you will ignore the last number in the array. Everything else looks fine to me.
Can someone please explain the thought process behind this code? I am kind of confused on how it works. This is the question that the code is addressing:
Write code (using one or more loops) to fill an array "a" with 10 different random numbers between 1 and 10.
Thank you so much for any help!
public static void main(String[] args){
//R8.8
int a[] = new int[10];
Random randomGenerator = new Random();
for (int i = 0; i < a.length; i++){
a[i] = 1 + randomGenerator.nextInt(100);
}
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
See my comments in the code itself:
public static void main(String[] args){
//make a new array of 10 integers
int a[] = new int[10];
//declare an object which we can use to generate random numbers
//this object probably uses the system time to generate numbers that appear random
//but at the end of the day, java does it for us so
//we don't really need to know or care how it generates random numbers
Random randomGenerator = new Random();
//loop over each element in our array
for (int i = 0; i < a.length; i++){
//for each element, set that element to a random between 1 and 100 inclusive
//nextInt(x) gets a number between 0 (inclusive) and x (not inclusive)
//so to translate that to 1 to x inclusive, we need to add 1 to the result
a[i] = 1 + randomGenerator.nextInt(100);
}
//everything below here does literally nothing to solve the problem
//everything you need to fill the array with random numbers is above
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
Please note that you should use 1 + randomGenerator.nextInt(10); to fill the array with numbers between 1 and 10, not 1 + randomGenerator.nextInt(100);.
I have this function for selection sort:
public static void selectionSort(int[] n)
{
for(int start = 0; start < n.length; start++)
{
int smallest = start;
for(int i = start+1; i<n.length; i++)
{if(n[i]<n[start])
smallest = i;}
int tmp = n[start];
n[start] = n[smallest];
n[smallest] = tmp;
}
}
and i call it like this.
Random ran = new Random();
int n = 10;
int[] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = ran.nextInt(1000);
However, it causes results like this..
640 900 610 168 650 610 527 356 802 486
486 640 356 168 610 527 610 650 802 900
the top one is not sorted the lower one is supposed to be sorted. However, it is not correct.
You are comparing the initial start index every singe iteration, even if you have found a smaller number you are still comparing it to the original start which should not happen. Once you find a smaller number you need to use that index for the comparison.
To compare it to the smallest number every iteration change (n[i]<n[start]) to (n[i]<n[smallest]) and that will fix your problem.
Hope this helps!
The second loop is useless because you are comparing, after all, just the first and the last item and smallest takes it's value from this comparison only.
By your code I guess you're trying to do a Bubble Sort, but the comparison and the index management is wrong, here is a posible solution:
public static void main(String[] args) {
Random ran = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = ran.nextInt(1000);
}
printArray(array);
selectionSort(array);
printArray(array);
}
private static void printArray(int[] array) {
System.out.print("Array: ");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
private static void selectionSort(int[] array) {
for (int j = 0; j < array.length; j++) {
// Subtract 1 so you don't get a NullPointerException
// Subtract j so you don't compare the numbers already ordered
for (int i = 0; i < array.length - 1 - j; i++) {
if (array[i] > array[i + 1]) {
int tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
}
}
}
}
The algorithm is taken from the Spanish version (I'm from Argentina) of the link above, which is also in the English version with other notation.
Hope this helps. Best regards
Try to use this version of code it's functional:
public static void main(String[] args) {
Random ran = new Random();
int n = 10;
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ran.nextInt(1000);
selectionSort(a);
for (int i : a) {
System.out.print(i+" ");
}
}
public static void selectionSort(int[] n)
{
for(int start = 0; start < n.length-1; start++)
{
int smallest = start;
for(int i = start+1; i<n.length; i++)
{if(n[i]<n[smallest])
smallest = i;}
int tmp = n[start];
n[start] = n[smallest];
n[smallest] = tmp;
}
}
package test1;
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int traincars;
int maxweight;
int count = 0;
int total = 0;
maxweight = input.nextInt();
traincars = input.nextInt();
int[] trains = new int[traincars];
for(int i = 0; i < traincars; i++)
{
trains[i] = input.nextInt();
}
if (total < maxweight)
{
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
}else
{
count = count + 3;
}
System.out.println("count");
}
}
this is a really simple program but for some reason, the array for the traincars goes out of bounds..
Why is this happening?
The problem is here:
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
When i equals traincars-1 you will be accessing elements i+1, i+2. and i+3 which are out of bounds of your trains array.
If your logic is calling for calculating totals of 4 consecutive elements of the array then your for loop should stop earlier:
for(int i = 0; i < traincars - 3; i++) {...}
In the last iteration of
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
You try to access trains[i+1] and this is bigger than the length of your trains array.
To make this for loop matter you should just do the following:
for(int i = 0; i < traincars; i++)
{
total += trains[i]; //unless of course you need something else...
count++;
}