我试图做一个交易或不交易的游戏,但是我现在的问题是随机为案例分配设置值。我没有随机生成一个数字并检查它是否已经存在,所以我现在试图简单地洗牌数组。问题是,出于某种原因,值重复。 代码:VB:Random Shuffler Duplicating Values
Dim nCaseValues(26) As Integer 'The different possible values for a case
Dim nCaseNumbers(26) As Integer 'The different case numbers
Dim nShadowNumber As Integer 'This holds the first number in the shuffle
Dim nShuffleNumber1 As Integer 'The first random position
Dim nShuffleNumber2 As Integer 'The second random position
'Clear the list if it already has content
listArray1.Items.Clear()
listArray2.Items.Clear()
listArray3.Items.Clear()
'Declare array for case values
nCaseValues(0) = 1500
nCaseValues(1) = 1
nCaseValues(2) = 2
nCaseValues(3) = 5
nCaseValues(4) = 10
nCaseValues(5) = 20
nCaseValues(6) = 50
nCaseValues(7) = 100
nCaseValues(8) = 150
nCaseValues(9) = 200
nCaseValues(10) = 250
nCaseValues(11) = 500
nCaseValues(12) = 750
nCaseValues(13) = 1000
nCaseValues(14) = 2000
nCaseValues(15) = 3000
nCaseValues(16) = 4000
nCaseValues(17) = 5000
nCaseValues(18) = 10000
nCaseValues(19) = 15000
nCaseValues(20) = 20000
nCaseValues(21) = 30000
nCaseValues(22) = 50000
nCaseValues(23) = 75000
nCaseValues(24) = 100000
nCaseValues(25) = 200000
'Declare array for case numbers
For genCaseNumArray = 0 To 25
nCaseNumbers(genCaseNumArray) = 0
listArray1.Items.Add(genCaseNumArray)
Next
'The shuffle
For J = 0 To 25 'This assigns case values to case numbers (It's probably moot, as I can just use the nCaseValues position, but it's here for now)
nCaseNumbers(J) = nCaseValues(J)
Next
For K = 0 To 25 'Lists the items below, this could possibly be an error
For I = 0 To 50 'Shuffles the list 50 times
nShuffleNumber1 = (Int(Rnd() * 26)) 'Gets a random number and assigns it
nShuffleNumber2 = (Int(Rnd() * 26))'Gets a random number and assigns it
nShadowNumber = nCaseNumbers(nShuffleNumber1) 'This holds the first value during the shuffle
nCaseNumbers(nShuffleNumber1) = nCaseNumbers(nShuffleNumber2)'First value now equals second value...
nCaseNumbers(nShuffleNumber2) = nShadowNumber 'And now second value holds first value.
Next 'Lists items in lists objects on the form
listArray1.Items.Add(K)
listArray2.Items.Add(nCaseValues(K))
listArray3.Items.Add(nCaseNumbers(K))
Next
我需要每个值是唯一的。我很确定这是我的代码中的逻辑错误。任何关于数字为什么重复的帮助都会很好。
这里有一对夫妇的输出:
第一清单的案件编号,二是为了可能的值,第三个是最终的结局 - 个案价值。正如你所看到的,一些是重复的,第一个列表重复两次..编辑:我发现它为什么重复两次 - 我很愚蠢,留下另一个循环,这也是添加到列表中。我已经解决了我的问题,请参阅下面的答案。
这只是不正确的方式来获得随机洗牌。你用一副52张牌做的方式是首先得到0到51之间的一个随机数。取出相应的牌。接下来得到0到50之间的一个随机数字,拿出卡片。接下来在0和49之间。等等,重复,直到甲板空了。 Google“vb.net fisher yates shuffle”,很多热门歌曲。 –