r.nextbytes(b) l = bitconverter.toint64(b,0) 无论如何,你可以调整你的程序来工作一个字节数组而不是一个long,甚至更好,生成随机字节并将它们转换为boolean数组,这样检查尾部/头部是微不足道的。 问候。 < bo ************ @ gmail.com> escribiónelel mensaje news:11 ******************** @ t31g2000cwb.googlegrou ps.com ... | (或者......我得到的尾巴太多而且没有足够的头部) | |我正在遇到一个非常奇怪的问题,随机数字和长 |数字。为了证明这个问题,我创建了一个简单的测试。 |考虑一系列硬币将被翻转。一次全部。 |组合翻转的结果是一系列位(0 =尾部或 | 1 =头部)。这些位形成一个数字,该数字可以表示为 |类型很长。 | |好。到目前为止还不错。要确定长,我需要知道的是 |许多硬币将被翻转: | | r = 2 ^ c | | r是表示翻转的最高值的长数字(所有 |翻转是1或者头部)。因此,值0表示0。意味着所有硬币翻转 |是尾巴。你懂了。现在要获得实际的翻转(0和 | 1s的混合),你只需: | | f = CLng(Rnd()* r) | |其中f现在从0到r很长。现在您需要做的就是去 |通过长f的每一位来确定每个硬币的结果 |被翻转(0101100101001 ......)。我们甚至写了一个不错的方法 |将每次翻转的结果显示为H。或T或T。让它变得容易 |可视化。 | |这一切都很有效,输出看起来像: | | [1]翻转:1 | [1]动词:H | [2]翻转:2 | [2]动词:HT | [3]翻转:5 | [3]动词:HTH | [4]翻转:5 | [4]动词:THTH | [5]翻转:10 | [5]动词:THTHT | |请记住H。 = 1和T = 0。所以在3个硬币的情况下 |翻转,结果为5(4 + 1)。这个例子增加了硬币数量 |每次翻转1,所以结果很容易看到和验证。 | | 那么问题是什么?你说?好吧,事情开始变得糟糕 |硬币#22。当硬币范围是 |时,看看结果是什么样的从1到50: | | [1]翻转:1 | [1]动词:H | [2]翻转:2 | [2]动词:HT | [3]翻转:5 | [3]动词:HTH | [4]翻转:5 | [4]动词:THTH | [5]翻转:10 | [5]动词:THTHT | [6]翻转:50 | [6]动词:HHTTHT | [7]翻转:2 | [7]动词:TTTTTHT | [8]翻转:195 | [8]动词:HHTTTTHH | [9]翻转:417 | [9]动词:HHTHTTT | [10]翻转:726 | [10]动词:HTHHTHTHHT | [11]翻转:93 | [11]动词:TTTTHTHHHTH | [12]翻页:1696 | [12]动词:THHTHTHTTTT | [13]翻转:7067 | [13]动词:HHTHHTHTHHTHH | [14]翻转:12951 | [14]动词:HTHTHTHTHTHHH | [15]翻转:12240 | [15]动词:THTHHHHHTHTTT | [16]翻转:63043 | [16]动词:HHHHTHTHTHTTTHH | [17]翻转:114222 | [17]动词:HHTHHHHTHTHHHH | [18]翻转:14742 | [18]动词:TTTTHHTHTHTHTHTHTH | [19]翻转:497841 | [19]动词:HHHTHTHTHTHTHTHTTH | [20]翻转:381701 | [20]动词:THTHHTHTHTTTTHTH | [21]翻转:1100729 | [21]动词:HTTTTHTHTHTHTHTHTHH | [22]翻页:3217500 | [22]动词:HHTTTHTTTHTHTTHTHHTHT | [23]翻页:448828 | [23]动词:TTTTHHTHTHTHTHTHHTHT | [24]翻转:9939800 | [24]动词:HTTHTHHHTHTHTHTHTHTT | [25]翻转:15726966 | [25]动词:THHHTHHHHHHHHTHTHHHTHH | [26]翻转:20009544 | [26]动词:THTTHTHTTHTTHTTHTTTT | [27]翻转:83576936 | [27]动词:HTTHHHHTHTHTHTTTHTHTTT | [28]翻转:173898176 | [28]动词:HTTHTHTHHHTHTHHTHTHTTTTT | [29]翻转:141622752 | [29]动词:THTTTTHHTHTTHHHHHTHHHTHTHTT | [30]翻页:299941248 | [30]动词:THTTTHHHTTTHTHHHTHTHTTTTT | [31]翻页:1781985408 | [31]动词:HTHTHTHTTHTHTHHTHTTTTTTTT | [32]翻页:3541639168 | [32]动词:HTHTHTTHTHTHTHTTTTTTTTTTT | [33]翻转:5060871680 | [33]动词:激动人心的动作 | [34]翻页:16940951552 | [34]动词:HHHHHTHTHTHTHTTTHTTTTTTTTTTT | [35]翻页:31300495360 | [35]动词:HTHTHTHTHTHTHTHTHTTTTTTTTT | [36]翻转:15590113280 | [36]动词:TTHHTTTTTHTHTHTTTTTTTTTT | [37]翻转:95535947776 | [37]动词:HTHTHTHHTHTHTHTHTHTHTHTHTTTTTTTTTTT | [38]翻转:269381238784 | [38]动词:HHHHTHTHTHTHTHTHTHTHHHHHHTHTTTTTTTTTTT | [39]翻页:134102679552 | [39]动词:TTHHHTHTHTHTTTTTTTTTTTTTT | [40]翻页:586999660544 | [40]动词:HTTTHTTHTHTHHTHHTTTTTTTTTTTTTTT | [41]翻页:233909387264 | [41]动词:TTTHTHTHTHTHTHTHTHTTTTHTTTTTTTTTTTTTT | [42]翻转:4395471732736 | [42]动词:HHHHHHHTHTHTHTHTTTTTTTTTTTTTTTT | [43]翻页:5947706048512 | [43]动词:哼哼哼哼哼哼哼哼噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢[44]翻转:276266221568 | [44]动词:TTTTTTTTTTTTTTTTTTTTTTTTTTTT | [45]翻转:20237481148416 | [45]动词:激动人心的任何东西是什么? | [46]翻转:7040550305792 | [46]动词:连接到这个问题的时间。 | [47]翻页:14499146891264 |动词:这是什[48]翻转:224865965572096 | [48]动词:重春节快要回复 | [49]翻页:160148156841984 | [49]动词:THTTHTTTHTTHTTTTTTTTTTTTT | [50]翻转:51396397236224 | [50]动词:TTTTHTHHHTHHHTHTHTHTHTHTHTTTTTTTTTT | | >从硬币#22开始,你不再得到奇数了,那是什么 |更糟糕的是,LSB都是0(Tails)。 | |以下是此测试中使用的程序: | |模块模块1 | Sub Main() | Dim c As New TestClass | Dim x,f As Long | Dim s As String |对于x = 1到50 | f = c.flip(x) | s = c.flip2string(x,f) | Console.WriteLine(" ["& x&"] flip:"& f.ToString) | Console.WriteLine(" ["& x&"] verb:"& s.ToString) |下一页 |结束子 |结束模块 | |这是它所称的课程: | |公共类TestClass |公共功能翻转(ByVal c As Integer)As Long | Dim r,f As Long |试试 | r = 2 ^ c | Catch ex As ArithmeticException | MsgBox(错误:& ex.ToString& vbCrLf&硬币:& | c.ToString&" r:"& r。 ToString) |结束尝试 | f = CLng(Rnd()* r) |如果f = r那么''结果被计为0。由于LSB / MSB |在Rnd舍入() | f = 0 |结束如果 |返回f |结束功能 | Public ReadOnly属性flip2string(ByVal c As Integer,ByVal f As | Long)As String |获取 | Dim s As String | Dim x As Integer |对于x = 1到c |如果getLSB(f)= 1那么 | s =H &安培; s |否则 | s =T &安培; s |结束如果 | f>> = 1''按位向右移动 |下一页 |返回s |结束获取 |结束财产 | Private ReadOnly Property getLSB(ByVal i As Long)As Integer |获取 |如果我和1然后''LSB设置(1)或H |返回1 |否则''LSB未设置(0)或T |返回0 |结束如果 |结束获取 |结束财产 |结束班 | |有什么想法吗? | | TIA!Hello bogusexception,The Rnd() function returns a Single, that is, a precision of 23 digits. You may want to generate a number using the Random class (http://msdn.microsoft.com/library/?u...asp?frame=true)To generate a Long, you may use:dim b(7) as byte, l as long''dim r as new random ''elsewherer.nextbytes(b)l=bitconverter.toint64(b,0)Anyway, you may adapt your program to work on an array of bytes instead of a long or, even better, generate the random bytes and convert them to an array of boolean, so that checking tail/head is trivial.Regards.<bo************@gmail.com> escribió en el mensaje news:11********************@t31g2000cwb.googlegrou ps.com...| (or.. "I''m getting too much Tails and not enough Heads")|| I''m running into a very strange problem with random numbers and long| numbers. To demonstrate the problem, I''ve created a simple test.| Consider that a series of coins are to be "flipped" all at once. The| result of the combined flip are a series of bits (0 = tails or| 1=heads). These bits form a number, and that number can be represented| by a type long.|| OK. Not so bad so far. To determine the long, all I need to know is how| many coins will be flipped:|| r = 2 ^ c|| r is the long number that represents the highest value of a flip (all| flips are 1, or heads). So a value of "0" means that all coin flips| were tails. You get it. Now to get the actual flip (a mix of 0s and| 1s), you just:|| f = CLng(Rnd() * r)|| Where f is now a long from 0 to r. Now all that you need to do is go| through each bit of the long f to determine the results of each coin| that was flipped ("0101100101001..."). We''ve even written a nice method| that displays the results of each flip as "H" or "T" to make it easy to| visualize.|| This all works wonderfully for a while, output looking like:|| [1] flip: 1| [1] verb: H| [2] flip: 2| [2] verb: HT| [3] flip: 5| [3] verb: HTH| [4] flip: 5| [4] verb: THTH| [5] flip: 10| [5] verb: THTHT|| Remember that "H" = 1 and "T" = 0. So in the case where 3 coins were| flipped, the result is 5 (4 + 1). This example increases the # of coins| flipped by 1 each time, so the results are easy to see and validate.|| "So whats the problem?" you say? Well, things start to go bad around| coin #22. Check out what the results look like when the coin range is| from 1 to 50:|| [1] flip: 1| [1] verb: H| [2] flip: 2| [2] verb: HT| [3] flip: 5| [3] verb: HTH| [4] flip: 5| [4] verb: THTH| [5] flip: 10| [5] verb: THTHT| [6] flip: 50| [6] verb: HHTTHT| [7] flip: 2| [7] verb: TTTTTHT| [8] flip: 195| [8] verb: HHTTTTHH| [9] flip: 417| [9] verb: HHTHTTTTH| [10] flip: 726| [10] verb: HTHHTHTHHT| [11] flip: 93| [11] verb: TTTTHTHHHTH| [12] flip: 1696| [12] verb: THHTHTHTTTTT| [13] flip: 7067| [13] verb: HHTHHHTTHHTHH| [14] flip: 12951| [14] verb: HHTTHTHTTHTHHH| [15] flip: 12240| [15] verb: THTHHHHHHTHTTTT| [16] flip: 63043| [16] verb: HHHHTHHTTHTTTTHH| [17] flip: 114222| [17] verb: HHTHHHHHTTTHTHHHT| [18] flip: 14742| [18] verb: TTTTHHHTTHHTTHTHHT| [19] flip: 497841| [19] verb: HHHHTTHHTTTHTHHTTTH| [20] flip: 381701| [20] verb: THTHHHTHTTHHTTTTTHTH| [21] flip: 1100729| [21] verb: HTTTTHHTTHTHHHTHHHTTH| [22] flip: 3217500| [22] verb: HHTTTHTTTHHTTTTHTHHHTT| [23] flip: 448828| [23] verb: TTTTHHTHHTHHTTHTTHHHHTT| [24] flip: 9939800| [24] verb: HTTHTHHHHTHTHTHHTHTHHTTT| [25] flip: 15726966| [25] verb: THHHTHHHHHHHHHTTHTHHHTHHT| [26] flip: 20009544| [26] verb: THTTHHTTTHTHTHTTHTTHTTHTTT| [27] flip: 83576936| [27] verb: HTTHHHHHTHHTHTTHTTTTHHTHTTT| [28] flip: 173898176| [28] verb: HTHTTHTHHHTHTHHHHTTHHHTTTTTT| [29] flip: 141622752| [29] verb: THTTTTHHHTTTTHHHHHHTHHHHTTTTT| [30] flip: 299941248| [30] verb: THTTTHHHHTTTTTHTHHHHTHHTTTTTTT| [31] flip: 1781985408| [31] verb: HHTHTHTTTHHTHHTHHHHTTTTHTTTTTTT| [32] flip: 3541639168| [32] verb: HHTHTTHHTTTHHTTHTTHTTTTTTTTTTTTT| [33] flip: 5060871680| [33] verb: HTTHTHHTHHTHTTHHTHHTTTHHTTTTTTTTT| [34] flip: 16940951552| [34] verb: HHHHHHTTTHHHTTTTHTTHHTHTTTTTTTTTTT| [35] flip: 31300495360| [35] verb: HHHTHTTHTTHHTHTTHHHHTHTHTTTTTTTTTTT| [36] flip: 15590113280| [36] verb: TTHHHTHTTTTHTTHHHHHTTHTTTTTTTTTTTTTT| [37] flip: 95535947776| [37] verb: HTHHTTTHHHHHTTHHTTTHTHHHTTTTTTTTTTTTT| [38] flip: 269381238784| [38] verb: HHHHHTHTHHHTTTTHTHHHHHHTTTTTTTTTTTTTTT| [39] flip: 134102679552| [39] verb: TTHHHHHTTHHHTTHTTHTTHTTHTTTTTTTTTTTTTTT| [40] flip: 586999660544| [40] verb: HTTTHTTTHTHTHTHHHHHTHTTTTTTTTTTTTTTTTTTT| [41] flip: 233909387264| [41] verb: TTTHHTHHTTHHHTHHTTTTHTHHTTTTTTTTTTTTTTTTT| [42] flip: 4395471732736| [42] verb: HHHHHHHHHHTHHTTHHTHTTTHTTTTTTTTTTTTTTTTTTT| [43] flip: 5947706048512| [43] verb: HTHTHHTHTTTHHTTHHHTHHHTHTTTTTTTTTTTTTTTTTTT| [44] flip: 276266221568| [44] verb: TTTTTHTTTTTTTHTHTTHTHHTTTTTTTTTTTTTTTTTTTTTT| [45] flip: 20237481148416| [45] verb: HTTHTTHHTTHHHHHHTTHHHHHHTTTTTTTTTTTTTTTTTTTTT| [46] flip: 7040550305792| [46] verb: TTTHHTTHHTTHHHTHTTTTTHHTTTTTTTTTTTTTTTTTTTTTTT| [47] flip: 14499146891264| [47] verb: TTTHHTHTTHTHHHHHHTHHTTTHTTTTTTTTTTTTTTTTTTTTTTT| [48] flip: 224865965572096| [48] verb: HHTTHHTTHTTTTTHHHTHHTTTTTTTTTTTTTTTTTTTTTTTTTTTT| [49] flip: 160148156841984| [49] verb: THTTHTTTHHTHTTHHHTHHTTHHTTTTTTTTTTTTTTTTTTTTTTTTT| [50] flip: 51396397236224| [50] verb: TTTTHTHHHTHTHHHHHTHTHTHTTTTTTTTTTTTTTTTTTTTTTTTTTT|| >From coin #22 on, you do not get odd numbers anymore, and what is| worse, the LSBs are all 0s ("T"ails).|| Here is the program used in this test:|| Module Module1| Sub Main()| Dim c As New TestClass| Dim x, f As Long| Dim s As String| For x = 1 To 50| f = c.flip(x)| s = c.flip2string(x, f)| Console.WriteLine("[" & x & "] flip: " & f.ToString)| Console.WriteLine("[" & x & "] verb: " & s.ToString)| Next| End Sub| End Module|| And here is the class it calls:|| Public Class TestClass| Public Function flip(ByVal c As Integer) As Long| Dim r, f As Long| Try| r = 2 ^ c| Catch ex As ArithmeticException| MsgBox("error: " & ex.ToString & vbCrLf & "coins: " &| c.ToString & " r: " & r.ToString)| End Try| f = CLng(Rnd() * r)| If f = r Then '' result is counted as "0" due to LSB/MSB| rounding in Rnd()| f = 0| End If| Return f| End Function| Public ReadOnly Property flip2string(ByVal c As Integer, ByVal f As| Long) As String| Get| Dim s As String| Dim x As Integer| For x = 1 To c| If getLSB(f) = 1 Then| s = "H" & s| Else| s = "T" & s| End If| f >>= 1 '' bitwise shift right| Next| Return s| End Get| End Property| Private ReadOnly Property getLSB(ByVal i As Long) As Integer| Get| If i And 1 Then '' LSB is set (1) or "H"| Return 1| Else '' LSB is not set (0) or "T"| Return 0| End If| End Get| End Property| End Class|| Any ideas?|| TIA! 这篇关于Rnd()vs. Long ......没有LSB的快乐!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 01:08