简体   繁体   English

用Java生成的随机数

[英]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. 在此之前,我导入了Random类,并创建了一个名为randomNumbers的新对象,并将变量frequency声明为具有7个整数的数组。 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. 我正在阅读的这本书告诉我,上面的这一行掷了600万次,并使用模具值作为频率指数。

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. 据我所知,这是在迭代中经历的600万次它是否在它每次在身体中所说的,但是我没有在身体中看到它将任何变量设置为等于随机数。 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. 我认为我遗漏的最重要的事情是增加频率[]意味着什么,因为据我所知,在括号内有一个1到6之间的随机数被加到1.所以600万次迭代应该按频率[ 1]通过频率[7]如果它有可能发生,但即使它通过它我也看不出它是如何设置任何等于那些数组的东西。

Can someone please explain this line of code to me step by step barney style? 有人可以一步一步向我解释这行代码barney风格吗? 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)返回0到5之间的数字。例如,如果它返回3,则添加1以使the_random_number变为4.之后,将频率出现4增加1并将其存储在frequency数组( frequency[4] = frequency[4] + 1; )。

randomNumbers.nextInt(6) generates a number from 0-5; randomNumbers.nextInt(6)生成0-5的数字; adding one makes it 1-6. 添加一个使它成为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. 如果随机数分布是偶数,则应该有一个大约有100万的数组。

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. 您还可以尝试使用Apache Commons Math库的RandomDataGenerator来生成随机数。

The Commons Math random package includes utilities for Commons Math随机软件包包括实用工具

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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