确定性bool数组 [英] Deterministic bool array

查看:100
本文介绍了确定性bool数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道如何在标题中描述,所以我希望我能在这里更好地解释它。基本上我想生成一个随机的8x8布尔值数组(我实际上会使用一个8字节的数组,或者一个64位长的数组并使用这些位作为标志)。问题是,我想要右下角的角落。数组的优先级最高。


让我们说我想限制真的数量可以包含在数组中的布尔值,我希望更多的布尔值朝向数组的右下角,而不是位于左下角,右上角和左上角。我想我可以将它限制为8个真值布尔值,然后迭代并随机选择是否打开某个布尔值或保持关闭。每次打开一个,我都会从计数中减去一个,当计数为0时我会打破循环。这个问题是我需要从右下角开始。把阵列想象成一个正方形,而布尔就像一个径向渐变,其中心位于正方形的右下角。右下角的渐变较暗,然后随着向其他角落移动而逐渐消失。较暗的部分是更有可能具有真值布尔值的地方,因为它逐渐消失,因为它们在较暗的区域中大部分用完,所以不太可能生成真值bool。这有意义吗?


基本上,我只需要知道如何迭代64位长(或8字节数组)的位并打开它们,但是以这样的方式产生我的梯度。类比。


有人能想出办法吗?我现在唯一可以想象的是,数组中最后一个字节的最后一位最有可能被打开;最后一个字节的第一位和第一个字节的最后一位将有相同的开启几率,第一个字节的第一位将有最小的可能性被打开。我无法理解如何编程。


顺便说一句,这必须是真实的,或尽可能接近真实,随机,因为我会每50毫秒左右再生一次。这样做的目的是让我可以画出这样的图片:

http://infectionist.com/images/halo_...ae/output1.gif


以编程方式用作加载屏幕。

解决方案

所以,如果我理解你的问题,这就是我如何解决你的问题。


喜欢你提到我会有一个计数变量来跟踪我打开了多少个方格。


当确定是打开还是关闭方格时,我会先得到一个随机数。然后我会将这个数字乘以一些百分比表示第一个90%,第二个80%。然后我会检查某个预定标记的值,如果它更大,那么我会打开或关闭它。对于循环的每次迭代,我乘以的百分比将变得更少。这样它是随机的,但它似乎也有利于首​​先检查的那些。


你发现 System.Collections.BitArray 对象对此有用吗?

问题是,我要么需要向后设置数组,要么将其向前设置并反转......或者将其旋转180度。


I really didn''t know how to describe that in the title, so I hope I can explain it better here. Basically I want to generate a random 8x8 array of boolean values (I''ll actually be using either an array of 8 bytes, or a single 64-bit long and using the bits as flags). The thing is, I want the bottom-right "corner" of the array to have the most precedence.

Let''s say that I want to limit the number of "true" booleans that can be contained in the array, and I want more booleans to be toward the bottom-right corner of the array and less to be in the bottom-left, top-right, and top-left corners. I was thinking that I could limit it to, say, 8 true-value booleans, then iterate through and randomly select whether to turn a certain boolean on or keep it off. Everytime I turn one on, I''d subtract one from the count, when the count is 0 I''d break the loop. The problem with this is that I need to start at the bottom right. Think of the array like a square, and the booleans like a radial gradient, the center of which is at the bottom-right corner of the square. The gradient is darker at the bottom right, then fades away as it moves toward the other coners. The darker section is where it''s more likely to have a true-value boolean, as it fades away it gets less likely that a true-value bool will be generated since they''ve been mostly used up in the darker area. Does that make sense?

So basically, I just need to know how to iterate through the bits of a 64-bit long (or an array of 8 bytes) and flip them on, but in such a way that will generate my "gradient" analogy.

Can anyone think of a way to do this? The only thing I can imagine right now is that the last bit of the last byte in the array will have the highest chance of being turned on; the first bit of the last byte and the last bit of the first byte will have equal chances of being turned on, and the first bit of the first byte will have the smallest possible chance of being turned on. I just can''t wrap my head around how I might program this.

And by the way, this does have to be truly, or as close to truly, random as possible, because I''ll be regenerating it every 50 milliseconds or so. The purpose of this is so that I can draw a picture like this:

http://infectionist.com/images/halo_...ae/output1.gif

Programmatically, for use as a loading screen.

解决方案

So if I understand your question correctly this is how I would solve your problem.

Like you mentioned I would have a count variable that keeps track of how many squares I have turned on.

When determining whether to turn a square on or off I would first get a random number. I would then multiply that number against some percentage say for the first one 90%, the second one 80%. I would then check that value against some predetermined mark and if it was greater then I would turn the bit on or turn it off. For each iteration of the loop the percentage I multiply against would get less. This way it is random, but it also seems favor the ones first checked.


You find the System.Collections.BitArray object useful for this?


The thing is, I either need to set the array backward, or set it forward and reverse it...or rotate it 180 degrees.


这篇关于确定性bool数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆