- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在创建一个程序,该程序应随机指定我将在 Java 期中考试中解决的问题。我创建了一个运行 100,000 次的程序,并将每个问题作为 HashMap 中的键输入,而它的值是从 100,000 中生成的计数数。我创建了以下简单程序:
public class randomChoice {
public static Map<Integer, Integer> dictionary = new HashMap<Integer, Integer>();
public static void randInt() {
Random rand = new Random();
int randomNum = rand.nextInt((33 - 1) + 1) + 1;
if (dictionary.containsKey(randomNum)) {
dictionary.put(randomNum, dictionary.get(randomNum) + 1);
} else {
dictionary.put(randomNum, 0);
}
}
public static void main(String[] args) {
int i = 0;
while (i < 100000) {
randInt();
i++;
}
System.out.println(dictionary);
Map.Entry<Integer, Integer> maxEntry = null;
for (Map.Entry<Integer, Integer> entry : dictionary.entrySet()) {
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}
}
System.out.println("\nThe question I will be using for the midterm is " + maxEntry.getKey() + " with a total count of " + maxEntry.getValue());
int total = 0;
for (Map.Entry<Integer, Integer> entry : dictionary.entrySet()) {
total = entry.getValue() + total;
}
System.out.println("\nTotal: " + total);
}
}
当我尝试将 HashMap 的所有值相加时(这是 100,000 个问题中每个问题的生成频率),我的问题就出现了。这是生成的输出,打印 HashMap,我将要做的随机选择的问题具有最高的值(value),最后是所有 HashMap 值的总和:
> run randomChoice
{1=3038, 2=3025, 3=3009, 4=2945, 5=2996, 6=3049, 7=3004, 8=3078, 9=3011, 10=3012, 11=2995, 12=3041, 13=3116, 14=3015, 15=3029, 17=3058, 16=3141, 19=3045, 18=2976, 21=2988, 20=3065, 23=2943, 22=3106, 25=3025, 24=3093, 27=3092, 26=3058, 29=3018, 28=2981, 31=3035, 30=2970, 32=3007, 33=3003}
The question I will be using for the midterm is 16 with a total count of 3141
Total: 99967
>
我的问题是,为什么总数是 99967 而不是 100,000?似乎有点可疑,它正好短了 33 个,我有 33 个问题可供选择。我在这里做错了什么?我的缺陷在哪里可以帮助我创建恰好 100,000 个生成的随机数?
最佳答案
您距离 100,000 正好差 33,因为当您第一次遇到 33 个 map 条目中的每一个时,您放置的是 0
,而不是 1
.这是一个差一错误。
改变
dictionary.put(randomNum, 0);
到
dictionary.put(randomNum, 1);
关于Java - 随机数生成器中的缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47799450/
我编写了一个函数来随机从 [-10,10] 中获取一对。 import System.Random main = do { s State g a randomSt = S
好的,我了解如何在 Scala 中实现随机数生成器以及如何设置生成的随机数的上限,但我对如何更改下限感到困惑。例如: var computerGuess= scala.util.Random
我写了一个函数来从 [-10,10] 中随机得到一对。 import System.Random main = do { s State g a randomSt = St
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在做一个项目,我需要在其中生成 8 个随机数。由于某种原因,我遇到随机数部分非常耗时的问题。 8 个随机数的意思是我需要一个由数字 0-9 组成的 8 个字符长的字符串。例如 01234567 或
这个问题已经有答案了: Why do I always get the same sequence of random numbers with rand()? (12 个回答) 已关闭 9 年前。
我看到这个问题可能已经在这里得到回答:Random using WELL512 但是,它对用户不太友好,也没有提供如何在“真实世界”的代码片段中使用它的示例。 这是我目前拥有的: #define m
我想知道是否有人可以为我澄清这一行。 Create a function die(x) which rolls a die x times keeping track of how many time
我正在制作一款有 6 名防守球员的足球比赛。我将这段代码设置为随机让他们都向四分卫移动。 我想知道是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有这么多 if 语句的情况下循环它,但我对
在以下位置:http://www.fredosaurus.com/notes-cpp/misc/random.html 它提到如果我们想生成一个1-10范围内的随机数,我们可以这样做: r = (ra
如何在 Linux 和 C++ 中使用随机数? 我找到了一些我想使用的代码,它有一行 srand((unsigned)time(0));//seed 但是 gcc 说 board.cpp:94:24:
这个问题在这里已经有了答案: Generating random whole numbers in JavaScript in a specific range (40 个答案) 关闭 9 年前。
我有以下脚本: Timer=0; function countdown(auctionid){ var auctions; var divs; Timer=Timer+1;
利用oracle的dbms_random包结合rownum来实现,示例如下,随机取499户: select * from ( select * from busi.t_ar_
我需要获取随机数,但它不应该等于之前的数字。这是我的一段代码。但这不起作用。 function getNumber(){ var min = 0; var max = 4; var i;
我对 Haskell 还很陌生。我有一个数据类型: data Sentence= Prop Int | No Sentence | And [Sentence]
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题已经有答案了: How do I generate random integers within a specific range in Java? (73 个回答) 已关闭 7 年前。
function getRandomArbitrary(min, max) { var r = Math.floor(Math.random() * (max - min + 1) + m
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Generate random number with non-uniform density 我尝试识别/
我是一名优秀的程序员,十分优秀!