尝试在一个数字(LUA)中生成多个数字间隔

发布于 2024-08-14 12:11:55 字数 536 浏览 4 评论 0原文

基本上我必须使用一个随机函数,它可以返回 0 到 2^16-1 之间的数字。

我正在尝试利用它并生成 1 和 6 之间的 6 个间隔(想想骰子扑克)。基本上使用为我随机生成的一个数字(我只能指定范围),并且最好只需要调用它一次。然后将这个数字“分成”6 个骰子,其中的值将在 1-6 之间。

我有点不确定人们会使用什么技术,想到的一件事是位屏蔽,但我不确定这是否是正确的上下文。

从二进制的角度来看,骰子值在 0-5 之间是合理的。这将为我们提供二进制数 000-101。现在,随机函数返回的最大值将为 101101101101101101(六个骰子,每个为“6”),以十进制表示,这意味着随机 int 返回的上限为 187245。下限为 0。现在让我们说random 函数返回数字 66508,将其转换为二进制形式,我们得到 010|000|001|111|001|100,但这还不够好!在这种情况下,其中一个骰子将是 111 = 7,当间隔应该是 0-5 (1-6) 时我们无法得到它:(

我需要找出一种方法来限制范围内的小数可以包含六个骰子,数值为 1-6 (0-5),但我需要一些帮助。

Basically I have to use a random function that can return a number between 0 and 2^16-1.

I am trying to make use of this and generate let's say, 6 intervals between 1 and 6 (thinking of dice poker). Basically use that one number that is randomly generated for me (I can specify range only) and preferably only have to call it once. Then take that number and "split" it into 6 dices where the values will be between 1-6.

I am a bit unsure of the technique one would use, one thing that comes to mind is bit masking but I am unsure if this is the right context.

From a binary point of view then it's reasonable to have the dice values between 0-5. That would give us the binary number 000-101. Now a max value of the random function return would be 101101101101101101 (six dices, each being "6") and in decimal it means the upper bound on the random int return would be 187245. The lower bound would be 0. Now let's say the random function returns us the number 66508, making it into binary form we get 010|000|001|111|001|100 and that's not good enough! In this case one of the dices would be 111 = 7 and we can't get that when the interval is supposed to be 0-5 (1-6) :(

I need to figure out a way a decimal within the limit range can contain six dices with the values 1-6 (0-5) but I am in some need of assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-08-21 12:11:55

您可以使用值x % 6(x / 6) % 6(x / 6^2) % 6、.. , (x / 6^5) % 6 其中 x 是随机数生成器中的一个数字。但如果你这样做的话,就会产生偏见。如果您想避免这种偏差,您应该拒绝 x 并在它大于 6**6-1 时重新选择。

You can use the values x % 6, (x / 6) % 6, (x / 6^2) % 6, ... , (x / 6^5) % 6 where x is a number from your random number generator. There will be a bias if you do this though. You should reject x and reselect if it is greater than 6**6-1 if you want to avoid this bias.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文