如何从定义的数字列表中生成数字,而不是一系列数字,即1-10

时间:2015-01-05 01:56:28

标签: vb.net

    Randomize()
    Dim gen_numb2 As Integer = CInt(Int((6 * Rnd()) + 1))
    Second_Number.Text = gen_numb2

enter image description here 我正在使用上面的代码生成数字,这些数字正在我的任务中创建的儿童数学程序中使用。我只需要知道如何让处理程序从我定义的列表中选择一个数字。我需要这样做,因为如果我为乘法或除法问题生成2个数字,对于10或11的孩子来说,72 x 32太难了。相反,我想生成第一个已经排序的数字,并且处理程序从一个数字列表中选择第二个数字,如1,2,5,10;因此,数学问题将更容易让孩子锻炼(32 x 2)。我确定它很简单,但我说它似乎只有人要求随机数字。

1 个答案:

答案 0 :(得分:1)

作为上述注释的评论者,我会用几个具有预定义值的构造来接近它,然后从它们中随机选择。

这里肯定有改进和改动的空间,但这应该[希望]让你开始:

' Values available for selection.
Dim values1 As Integer() = {1, 2, 3, 4, 5}
Dim values2 As Integer() = {16, 32, 64}

' Randomize the selection.
Dim randomizer As New Random

' Pick a value from each array.
Dim value1 As Integer = values1(randomizer.Next(values1.Length))
Dim value2 As Integer = values2(randomizer.Next(values2.Length))

' Present the problem.
Value1TextBox.Text = value1
Value2TextBox.Text = value2
' Store the answer in the Tag property so we can get to it easily.
AnswerTextBox.Tag = value1 * value2