简体   繁体   English

查询Math.random以仅生成所需数量的每个数字

[英]query for Math.random to generate only a certain amount of each number needed

I am curious if anyone knows a way to use math.random to generate random numbers between say 0 and 3, But when it generates two 0's or two 1's it rules out the possibility of generating them numbers? 我很好奇是否有人知道使用math.random生成介于0和3之间的随机数的方法,但是当它生成两个0或两个1时,它是否排除了生成数字的可能性?

This is for a game assignment for college and all I have left to do is set it that it only generates two of each number with one 3. If anyone knows how this would be very helpful (even if it is using something other than math.random . 这是为大学分配的游戏,我所要做的就是将它设置为仅生成每个数字中的两个带有1的3。如果有人知道这将非常有帮助(即使使用的不是math.random

The language is Java. 语言是Java。

So, you basically want to keep track of the number draws, right? 所以,您基本上想跟踪抽奖,对吧?

A possible solution is to use an array whose length is the valid range of your random numbers, where each cell counts the occurrences of the respective number. 一种可能的解决方案是使用长度为您的随机数有效范围的数组,其中每个单元格都会计算相应数字的出现次数。 Then, for each draw, you check the contents of the respective cell in the array to see if you reached the limit. 然后,对于每次绘制,您检查数组中各个单元的内容,以查看是否达到极限。 If you did, then redraw and repeat. 如果您这样做了,请重画并重复。

Note: at the time of writing this, the language of use is unknown, but the solution is generic enough to be implemented in virtually any language that provide a random() function. 注意:在撰写本文时,使用的语言尚不清楚,但是该解决方案足够通用,几乎可以用任何提供random()函数的语言来实现。

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

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