如何随机使用另一个随机数VB.NET进行删除

时间:2015-09-15 23:23:29

标签: vb.net windows-phone-8.1

每次用户打开应用程序时,我都试图使按钮的内容成为随机数。这是迄今为止的代码。

 Dim rndnumber As Random
    Dim number1 As Integer
    Dim number2 As Integer
    Dim number3 As Integer
    Dim number4 As Integer
    Dim number5 As Integer
    Dim number6 As Integer
    Dim number7 As Integer
    Dim number8 As Integer
    Dim number9 As Integer
    Dim number10 As Integer
    Dim number11 As Integer
    Dim number12 As Integer
    Dim number13 As Integer
    Dim number14 As Integer
    Dim number15 As Integer
    Dim number16 As Integer
    Dim number17 As Integer
    rndnumber = New Random

    Do
        number1 = rndnumber.Next(0, 18)
        number2 = rndnumber.Next(0, 18)
        number3 = rndnumber.Next(0, 18)
        number4 = rndnumber.Next(0, 18)
        number5 = rndnumber.Next(0, 18)
        number6 = rndnumber.Next(0, 18)
        number7 = rndnumber.Next(0, 18)
        number8 = rndnumber.Next(0, 18)
        number9 = rndnumber.Next(0, 18)
        number10 = rndnumber.Next(0, 18)
        number11 = rndnumber.Next(0, 18)
        number12 = rndnumber.Next(0, 18)
        number13 = rndnumber.Next(0, 18)
        number14 = rndnumber.Next(0, 18)
        number15 = rndnumber.Next(0, 18)
        number16 = rndnumber.Next(0, 18)
        number17 = rndnumber.Next(0, 18)
    Loop Until number1 IsNot number2 Or number3 Or number4 Or number5 Or number6 Or number7 Or number8 Or number9 Or number10 Or number11 Or number12 Or number13 Or number14 Or number15 Or number16 Or number17

    button1.Content = number1.ToString
    button2.Content = number2.ToString
    button3.Content = number3.ToString
    button4.Content = number4.ToString
    button5.Content = number5.ToString
    button6.Content = number6.ToString
    button7.Content = number7.ToString
    button8.Content = number8.ToString
    button9.Content = number9.ToString
    button10.Content = number10.ToString
    button11.Content = number11.ToString
    button12.Content = number12.ToString
    button13.Content = number13.ToString
    button14.Content = number14.ToString
    button15.Content = number15.ToString
    button16.Content = number16.ToString
    button17.Content = number17.ToString

这是17个按钮中的第一个,我对每个按钮都有相同的代码,但是已经对#34;循环进行了分支,直到number1不是数字2"部分匹配每个不同的数字。

问题出在" Isnot"部分在循环中。

我怎样才能理解每个rndnumber的价值?

先谢谢。

3 个答案:

答案 0 :(得分:0)

注意:我不使用windows-phone,所以这个答案通常与.Net相关,可能不适用

要以这种方式工作,您的代码必须具有类似的大型条件(变量缩短):

Loop While n1 <> n2 AndAlso n1 <> n3 ... AndAlso n1 <> n17 AndAlso n2 <> n3 ... AndAlso n16 <> n17

这是16 + 15 + ... + 1 = 136条件写入和满足;很有可能你会在那个循环中被困住一段时间。

更简单的方法是使用您的值构造一个数组,然后将其洗牌;类似的东西:

' suppose a Random instance named "rnd" created somewhere
Dim values = Enumerable.Range(0, 18).OrderBy(Function(unused) rnd.NextDouble).ToArray

button1.Content = values(0).ToString
' same for other button

也许还应该可以使用某种循环来填充按钮内的值。

答案 1 :(得分:0)

您可以将每个随机数添加到列表中,然后在Loop While条件中添加以下内容:rndList.Count <> rndList.Distinct.ToList.Count

这是一个快速的上午1点:

Dim rndnumber As Random = New Random
Dim rndList As List(Of Integer)

Do
    rndList = New List(Of Integer)
    For i = 1 To 17
        rndList.Add(rndnumber.Next(0, 18))
    Next
Loop While rndList.Count <> rndList.Distinct.ToList.Count

button1.Content = rndList(0).ToString
button2.Content = rndList(1).ToString
button3.Content = rndList(2).ToString
button4.Content = rndList(3).ToString
button5.Content = rndList(4).ToString
button6.Content = rndList(5).ToString
button7.Content = rndList(6).ToString
button8.Content = rndList(7).ToString
button9.Content = rndList(8).ToString
button10.Content = rndList(9).ToString
button11.Content = rndList(10).ToString
button12.Content = rndList(11).ToString
button13.Content = rndList(12).ToString
button14.Content = rndList(13).ToString
button15.Content = rndList(14).ToString
button16.Content = rndList(15).ToString
button17.Content = rndList(16).ToString

答案 2 :(得分:0)

此方法假定您的按钮的名称如图所示。它是一种.Net Forms方法,但可能会被修改为与Phone一起使用。

    Dim rndnumber As New Random
    Dim butContent As List(Of Integer) = Enumerable.Range(1, 17).ToList

    For x As Integer = 1 To 17
        Dim idx As Integer = rndnumber.Next(butContent.Count)
        Dim cntnt As String = butContent(idx).ToString
        butContent.RemoveAt(idx)
        Me.Controls("button" & x.ToString).Text = cntnt
    Next