简体   繁体   中英

Random Number Generating in Java

I am new to programming and have just started to learn about arrays and was doing just fine until I got to an array that stores random numbers inside of it. The line of code below is the one that baffles me the most.

for (int roll = 1; roll <=6000000; roll++)
     ++frequency[1 + randomNumbers.nextInt(6)];

Before this I imported the Random class and made a new object named randomNumbers as well as declared the variable frequency as an array with 7 integers. The book I am reading tells me that this line above rolls a die 6 million times and uses the die value as frequency index.

I understand that this is going through in iteration 6 million times were it does what it says in the body each time, however I do not see in the body where it sets any variable equal to a random number. I think the biggest thing I am missing is what it means to increment the frequency[] because I understand that within the brackets there is a random number between 1 and 6 being added to 1. So therefore the six million iterations should pass by frequency[1] through frequency[7] if it chance to happen, yet even though it passes them by I do not see how it sets anything equal to those arrays.

Can someone please explain this line of code to me step by step barney style? I just can't seem to wrap my head around it.

That routine could be broken down into this

for (int roll = 1; roll <=6000000; roll++) {
    int the_random_number = 1 + randomNumbers.nextInt(6);
    frequency[the_random_number] = frequency[the_random_number] + 1;
}

The code randomNumbers.nextInt(6) returns a number between 0 and 5. For example, if it returns 3, then 1 is added so the_random_number becomes 4. After that you increase the frequency occurence of 4 by 1 and store it in the frequency array ( frequency[4] = frequency[4] + 1; ).

randomNumbers.nextInt(6) generates a number from 0-5; adding one makes it 1-6. The ++ increments the value in the results array corresponding to the chosen number.

You should have an array with approximately 1 million in each if the random number distribution is even.

Use these sample as your reference:
import java.util.Random;

/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {

public static final void main(String... aArgs){
log("Generating 10 random integers in range 0..99.");

//note a single Random object is reused here
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
  int randomInt = randomGenerator.nextInt(100);
  log("Generated : " + randomInt);
}

log("Done.");
}

private static void log(String aMessage){
System.out.println(aMessage);
}
}

You can also try Apache Commons Math library's RandomDataGenerator to generate random numbers.

The Commons Math random package includes utilities for

generating random numbers generating random vectors generating random strings generating cryptographically secure sequences of random numbers or strings generating random samples and permutations analyzing distributions of values in an input file and generating values "like" the values in the file generating data for grouped frequency distributions or histograms

Example

RandomDataGenerator randomData = new RandomDataGenerator(); 
randomData.reSeedSecure(1000);
for (int i = 0; i < 1000; i++) {
    value = randomData.nextSecureLong(1, 1000000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM