How can I check if every single int in a randomly generated array is even and make it create another random array if it's not?

find elements which are present in first array and not in second in c
knuth shuffle
shuffle array c++
fisher-yates shuffle javascript
matlab random number between 0 and 1
shuffle array java
shuffle array in c
shuffle array - leetcode

So I'm trying to create a program that creates a randomly generated array with numbers between 0 and 10. Every time a number inside the 4x4 array is odd I want it to generate a brand new array and print every array discarded aswell until it creates a 4x4 array with only even numbers. The problem right now is that I can't understand how to fix the last for and make it work properly with the boolean b that is supposed to restart the creation of the array.

import java.util.Scanner;

public class EvenArrayGenerator {
    public static void main(String a[]) {

        Boolean b;
        do {
            b = true;
            int[][] Array = new int[4][4];
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++)
                    Array[i][j] = (int) (Math.random() * 11);
            }

            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    System.out.print(Array[i][j] + " ");

                }
                System.out.println();
            }
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (Array[i][j] % 2 != 0)
                        b = false;

                }
            }

        } while (b);
    }
}
public class ArrayGen {

private int[][] array = new int[4][4];
    private int iterations = 1;  // you always start with one iteration

public static void main (String[] args) {

    ArrayGen ag = new ArrayGen();

    ag.reScramble();

    while(!ag.isAllEven()) {

        ag.reScramble();
        ag.iterations++;
    }

    // this is just a nice visualisation

    for (int i = 0; i < 4; i++) {

        System.out.print("[");

        for (int j = 0; j < 4; j++) {

            System.out.print(ag.array[i][j] +((j != 3)? ", " : ""));
        }

        System.out.print("]\n");
    }
    System.out.println(ag.iterations + " iterations needed to get all-even array.");
}

private void reScramble () {

    for (int i = 0; i < 4; i++) {

        for (int j = 0; j < 4; j++) {

            array[i][j] = (int)(Math.random() * 11);
        }
    }
}

private boolean isAllEven () {

    for (int i = 0; i < 4; i++) {

        for (int j = 0; j < 4; j++) {

            if (array[i][j] % 2 == 1) {

                return false;
            }
        }
    }

    return true;
}
}

I think this is a good solution. Refactoring your code into structured methods is never a bad idea. I hope this helps!

Picking a Random Item from an Array, How do you randomly select a number in an array? Make a random number between 3, for every generate number it is greater than or equal, increase the created number by 1. lets say the number is 2, and you want to generate another: Generated numbers: [1,2,3] Possible number: [1,3,4] Solution:

You are looping until you get an array that's all even. You should initialize b to be false, and update it to true in the (nested) for loop. Note that once's you've set it to false, there's no reason checking the other members of the array, and you can break out of the for loop.

Note, also, that using stream could make this check a tad more elegant:

b = Arrays.stream(arr).flatMapToInt(Arrays::stream).anyMatch(x -> x % 2 != 0)

Fill an array with random numbers, How do you create an array of random numbers in Java? I just created an array with 100 initialized values and I want to print out 10 elements on each line so it would be somthing like this 0 1 2 3 4 5 6 7 8 9 10 11 12 13

What about generating random numbers up to 5 and double it? Then you don't have two check if they are even.

How to generate random numbers in Java without repetition, How do you generate a random number without duplicates in Java? Then you can easily check if the map contains the key or not. This is useful if you need the behavior of a set, where if you add a value multiple times it's only in the set once. Here's a simple example where I add random numbers as keys to a map.

Instead of your last for loop:

for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
        if(Array[i][j] % 2!=0){
            b=false;
            break;
        }
    }
    if(!b){
        break;
    }
}
if(!b){
    break;
}

Alternatively, you could do an oddity check when you are generating the elements. Something like:

int element;    
for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
        do{
            element = (int)(Math.random()*11);
        }while(element % 2 !=0)
        Array[i][j] = element;
    }
}

That way you don't have to check the values, they will always be even.

Beginning Arduino Programming, Flicker Example Code int ledPin = 11; int flicker[] = {64, 22, 4, 28, 6, 130, 186, 120​}; will be randomly determined to be between a tenth to one half of a second in length. The array flicker[] contains eight elements with values between 0 and 255 to creating a more lifelike behavior that makes our flickering LED even more  Notice the last method calls getDigitsOf method too much time. So it will be slower. You should create an int array and then call the getDigitsOf method once, just like in second code block. In the following code, you can reverse to process. This code puts all digits together to make the number:

This should work:

import java.util.Scanner;
public class EvenArrayGenerator{
    public static void main(String a[]){
        boolean anyOdd;
        int array = 0;

        do{
            System.out.println ("Array " + ++array + ":");
            anyOdd=false;
            int[][] Array = new int[4][4];
            for(int i=0;i<4;i++) {
                for(int j=0;j<4;j++) {
                    Array[i][j] = (int)(Math.random()*11);
                }
            }

            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    System.out.print(Array[i][j] + " ");
                }
                System.out.println();
            }

            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    anyOdd |= Array[i][j] % 2!=0;
                }
            }

        } while(anyOdd);
    }
}

As you can see, I just modified the condition from b to anyOdd, so if there is any odd number, it will iterate again.

Also, you can check it when you generate the random numbers, so you avoid a second loop:

import java.util.Scanner;
public class EvenArrayGenerator{
    public static void main(String a[]){
        boolean anyOdd;
        int array = 0;

        do{
            System.out.println ("Array " + ++array + ":");
            anyOdd=false;
            int[][] Array = new int[4][4];
            for(int i=0;i<4;i++) {
                for(int j=0;j<4;j++) {
                    Array[i][j] = (int)(Math.random()*11);
                    anyOdd |= array[i][j] % 2 != 0;
                }
            }

            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    System.out.print(Array[i][j] + " ");
                }
                System.out.println();
            }

        } while(anyOdd);
    }
}

Introduction to Programming Using Processing, Second Edition, Write a single line of Processing code to accomplish each of the following: (a) named images for an array of PImage objects, and create an array that can (d) Declare a reference variable named ints for an array of 10 int elements, Write a Processing method to return the sum of all the even elements in an array of  For even faster random number generation, a "Skip" setting of 2 or even 1 could be used, but the quality of the random numbers will then decline. The "Start" option tied with a larger "Size" and "Skip" is useful for setting up a family of independent generators that can be used in parallel computations.

Adobe Flash CS3: The Professional Portfolio, $U$t If Working uiith Variables and Arrays When you last tested your movie, you You might have noticed, however, that the racers simply keep running even after throughout the race, and another to determine which character wins the race. of this functionality, you will use variables and randomly generated numbers. Every time the button gets pressed, a random whole number needs to be generated. The number will be used as a representation of the array index number for the quote array. Once we retrieve the randomly picked quote from the array using our randomly generated whole number, we will place it to the HTML document.

Shuffle a given array using Fisher–Yates shuffle Algorithm , Here shuffle means that every permutation of array element should equally likely. we are given a function rand() that generates random number in O(1) time. swap it with a randomly selected element from the whole array (including last). a of n elements (indices 0..n-1): for i from n - 1 downto 1 do j = random integer  Output : Repeating elements are 4 2. Time Complexity: O(n*n) Auxiliary Space: O(1) Method 2 (Use Count array) Traverse the array once. While traversing, keep track of count of all elements in the array using a temp array count[] of size n, when you see an element whose count is already set, print it as duplicate.

Find elements which are present in first array and not in second , Given two arrays, the task is that we find numbers which are present in first A Naive Approach is to use two loops and check element which not present in second array. int j;. for (j = 0; j < m; j++). if (a[i] == b[j]). break ;. if (j == m). cout << a[i] << " " ;. } One by one check all elements of first array and print all those elements  Array length. Once we create an array, its length is fixed. You can refer to the length of an a[] in your program with the code a.length. Default array initialization. For economy in code, we often take advantage of Java's default array initialization convention.

Comments
  • Thanks, this is much better than what I did, I usually dislike structured methods but it's definitely more readable
  • or you could avoid the flat mapping completely and just go with double anyMatch --> Arrays.stream(arr).anyMatch(a -> Arrays.stream(a).anyMatch(x -> x % 2 != 0));. flat mapping is not lazy until JDK 10 if i remeber correctly.
  • Wow thanks, that has been very useful and it fixed my program perfectly, I didn't know that I could use stream
  • I want to calculate the number of iterations needed to create this random even array and put them in a diagram showing statistics
  • That should be pretty easy math, @axaro1: 6 even, 5 uneven, so 6/11 for each number; 4*4 = 16 numbers, the change of it being all evens is 6/11^16; so 1 in 7.6582883e15 (that's 7.658.288.300.000.000)
  • I'm just trying to have some fun programming to prepare myself for an exam, since I made something similar with a program creating 4 dice until they all have the same number I just wanted to make something a little bit bigger.