Home java Java Math.Random repeats values. Galton Board

Java Math.Random repeats values. Galton Board

Author

Date

Category

This program demonstrates the work of the Galton Board.
This board has N pcs. Slot below and one hole on top.
The balls are served on top that randomly fall into one of the N slots.
The board is used to explore the probability theory.

See here https://ru.wikipedia.org/wiki/Doska_Galton

During testing, it was revealed that the methods of Math.Random (), random.nextint (), random.nextBoolean () , etc.
Return the same results.


Import java.util.arrays;
Import java.util.random;
Import java.util.scanner;
Public Class Main {
  Public Static Scanner Scan = New Scanner (System.in);
  // Random number generator
  Public Static Random Rand = New Random ();
  Public Static Void Main (String [] Args) {
    // Slots
    int [] slots;
    // Number of slots
    int n;
    // Number of Sharikov
    INT BALLS;
    // shows what way to roll the False ball - left, true - right
    Boolean Side;
    // The slot index in which the current ball should get
    int k;
    // Middle Massiva
    INT MIDDLE;
    // Number of repetition of the experiment
    Int Times;
    // Shows the largest number of balls in one slot
    INT MAX;
    // initialization
    System.Out.print ("ENTER NUMBER OF REPETTIONS:");
    Times = scan.nextint ();
    System.out.print ("ENTER NUMBER OF BALLS:");
    Balls = scan.nextint ();
    System.out.print ("ENTER NUMBER OF SLOTS:");
    n = scan.nextint ();
    slots = new int [n];
    Middle = N / 2;
    While (Times & GT; 0) {
      While (Balls & gt; 0) {
        k = 0;
        For (int i = 0; i & lt; n - 1; i ++) {
          // "Random" True or False, on which the values ​​of K
          Side = Rand.NextBoolean ();
          K = (Side)? k + 1: k - 1;
        }
        // Since 2 ball riding correspond to one index change
        // Delim on 2, Identity Algebraically
        k = middle + k / 2;
        // Add a ball in the slot, reducing the number of the remaining balls
        SLOTS [K] ++;
        Balls--;
      }
      // Find a maximum draw
      Max = Max (Slots);
      While (Max & gt; 0) {
        For (int slot: Slots) {
          IF (Slot & gt; = max) {
            System.out.print (O ");
          } else {
            System.out.print ("");
          }
        }
        System.Out.printLN ();
        Max--;
      }
      System.out.PrintLN (Arrays.tostring (Slots) + "\ n");
      Times--;
    }
  }
  Public Static Int Max (int [] Array) {
    int max = 0;
    For (int i = 1; i & lt; array.length; i ++) {
      if (Array [Max] & lt; array [i]) {
        max = i;
      }
    }
    RETURN ARRAY [MAX];
  }
}

Despite the fact that every time you start the program, different random values ​​are obtained,
During the program execution, when reducing the experiment in the cycle, the methods return the same
Values ​​with the same sequence as at first attempt.

Here I repeat the experiment 2 times, but the 2nd experiment does not differ from the 1st.
Why is it so, what is it connected with and how to always get unique meanings?

p.s.
I tried

public static random Rand = New Random (System.CurrentTimemillis ());

and in the cycle of attempts

while (Times & GT; 0) {
    // Code ...
    Rand = New Random (System.CurrentTimeMillis ());
  }
  // Code ...

or

while (Times & GT; 0) {
    // Code ...
    Rand.setSeed (System.CurrentTimemillis ());
  }
  // Code ...

did not help


Answer 1, Authority 100%

You need to initialize the random number generator.

Random Rand = New Random (System.CurrentTimeMillis ());

By default, initialization goes the same number.

But the main problem is different and you would easily see her if they launched the program step by step.

See:

// add a ball in a slot, reduce the number of the remaining balls
        SLOTS [K] ++;
        Balls--;

What will be equal to Balls after the first passage of the cycle? It is clear that zero and with the second pass, you simply printed the contents of SLOTS.

Since the number of balls at the beginning of each passage should be equal to the keyboard, and the slots must be empty, then the initialization should be carried out before the process of imitation of the fall of the ball.

Here is a corrected code.

import java.util.arrays;
Import java.util.random;
Import java.util.scanner;
Public Class Main {
  Public Static Scanner Scan = New Scanner (System.in);
  // Random number generator
  Public Static Random Rand = New Random (System.CurrentTimemillis ());
  Public Static Void Main (String [] Args) {
    // Slots
    int [] slots;
    // Number of slots
    int n;
    // Number of Sharikov
    INT BALLS;
    // shows what way to roll the False ball - left, true - right
    Boolean Side;
    // The slot index in which the current ball should get
    int k;
    // Middle Massiva
    INT MIDDLE;
    // Number of repetition of the experiment
    Int Times;
    // Shows the largest number of balls in one slot
    INT MAX;
    // initialization
    System.Out.print ("ENTER NUMBER OF REPETTIONS:");
    Times = scan.nextint ();
    System.out.print ("ENTER NUMBER OF BALLS:");
    Balls = scan.nextint ();
    INT BALLS_CURRENT;
    System.out.print ("ENTER NUMBER OF SLOTS:");
    n = scan.nextint ();
    Middle = N / 2;
    While (Times & GT; 0) {
      Balls_current = Balls;
      slots = new int [n];
      While (Balls_Current & GT; 0) {
        k = 0;
        For (int i = 0; i & lt; n - 1; i ++) {
          // "Random" True or False, on which the values ​​of K
          Side = Rand.NextBoolean ();
          K = (Side)? k + 1: k - 1;
        }
        // Since 2 ball riding correspond to one index change
        // Delim on 2, Identity Algebraically
        k = middle + k / 2;
        // Add a ball in the slot, reducing the number of the remaining balls
        SLOTS [K] ++;
        Balls_Current--;
      }
      // Find a maximum draw
      Max = Max (Slots);
      While (Max & gt; 0) {
        For (int slot: Slots) {
          IF (Slot & gt; = max) {
            System.out.print (O ");
          } else {
            System.out.print ("");
          }
        }
        System.Out.printLN ();
        Max--;
      }
      System.out.PrintLN (Arrays.tostring (Slots) + "\ n");
      Times--;
    }
  }
  Public Static Int Max (int [] Array) {
    int max = 0;
    For (int i = 1; i & lt; array.length; i ++) {
      if (Array [Max] & lt; array [i]) {
        max = i;
      }
    }
    return array [max];
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions