随机数之和必须大于x且小于y

时间:2019-01-19 21:39:54

标签: vb.net random

我希望我的代码表示生成的随机数之和在一定范围内。在120至235之间。
无需过多更改我的代码的最佳方法是什么?

我很肯定它需要创建2个Dims和一个if else语句,但是我不能正确地用词表达。
我正在使用Visual Studio 2017

Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
        Randomize()

        TextBox1.Text = Rand(1, 100)
        TextBox2.Text = Rand(Long.Parse(TextBox1.Text), 100)
        TextBox3.Text = Rand(Long.Parse(TextBox2.Text), 100)
        TextBox4.Text = Rand(Long.Parse(TextBox3.Text), 100)
        TextBox5.Text = Rand(Long.Parse(TextBox4.Text), 100)
        TextBox6.Text = Rand(Long.Parse(TextBox5.Text), 100)
    End Sub

    Private Function Rand(v As Long) As String
        Throw New NotImplementedException()
    End Function

    Private Function Rand(ByVal Low As Long, ByVal High As Long) As Long
        Rand = Int((High - Low + 1) * Rnd()) + Low
    End Function
End Class

2 个答案:

答案 0 :(得分:1)

我建议使用.Net Random class生成随机数。它也更易于使用。

找到介于0和最小值之间的第一个随机数,然后第二个随机数将在以下范围内: randomMin(MinValue) => (MinValue - randomMin, MaxValue - randomMin)

randomMin = rnd1.Next(Min + 1)
randomMax = rnd2.Next(Min - randomMin, Max - randomMin + 1)
result = randomMin + randomMax

要记住,在Random类中,上限是互斥的,因此我们需要在Max值上加上1,以将其包括在随机值范围内。


进行样本测试:
这些代码示例假定所使用的VB.Net版本至少为V.14,VS 2015 + 。)

Private rnd1 As Random = New Random()
Private rnd2 As Random = New Random()

'(...)

Dim Min As Integer = 120
Dim Max As Integer = 235

For i = 0 To 100
    Dim randomValues = GetRandomNumbersInRange(Min, Max)
    Console.WriteLine($"Random Min: {randomValues.rndMin} Random Max {randomValues.rndMax}")
    Console.WriteLine($"Sum: {randomValues.rndMin + randomValues.rndMax}")
Next

'(...)

Private Function GetRandomNumbersInRange(Min As Integer, Max As Integer) As 
                                        (rndMin As Integer, rndMax As Integer)
    Dim randomMin As Integer = rnd1.Next(Min + 1)
    Return (randomMin, rnd2.Next(Min - randomMin, Max - randomMin + 1))
End Function

如果您希望方法直接返回总和,则可以这样更改方法返回类型:

Dim Min As Integer = 120
Dim Max As Integer = 235

For i = 0 To 100
    Console.WriteLine(GetSumRandomNumbersInRange(Min, Max))
Next

'(...)

Private Function GetSumRandomNumbersInRange(Min As Integer, Max As Integer) As Integer
    Dim randomMin As Integer = rnd1.Next(Min + 1)
    Return randomMin + rnd2.Next(Min - randomMin, Max - randomMin + 1)
End Function

还可以通过以下方式选择随机数:
randomMid(MaxValue - MinValue) => (MinValue, MaxValue - randomMid)

在这种情况下,可能实现为:

Private Function GetSumRandomNumbersInRange2(Min As Integer, Max As Integer) As Integer
    Dim randomFirst As Integer = rnd1.Next(Max - Min + 1)
    Return randomFirst + rnd2.Next(Min, Max - randomFirst + 1)
End Function

答案 1 :(得分:0)

这是我用来完成此任务的算法:

  1. 生成一个介于120到235之间的随机数。这将是最终的总和。
  2. 生成0.0到1.0范围内的六个随机数。
  3. 对这六个数字进行归一化,即将每个数字除以所有六个数字的和。
  4. 将六个归一化数字中的每一个乘以原始随机数。
  5. 舍弃六个结果中的每一个。

您现在有六个随机数,总和在所需范围内。您可能只需要在其中一个值上加上或减去1,以防四舍五入使总和超出范围。您可以随机选择一个数字进行调整。

'Create a random number generator.
Dim rng As New Random

'Create a random number in the desired range for the final sum.
Dim sum = rng.Next(120, 235 + 1)

'Generate six proportional values as fractions of 1.0.
Dim proportions = Enumerable.Range(1, 6).Select(Function(n) rng.NextDouble()).ToArray()

'Get the sum of all the proportional values.
Dim proportionsSum = proportions.Sum()

'Normalise the proportional values so that they sum to 1.0
proportions = Array.ConvertAll(proportions, Function(r) r / proportionsSum)

'Break the final sum up into the specified proportions.
Dim numbers = Array.ConvertAll(proportions, Function(r) CInt(Math.Round(r * sum)))

'Adjust as required if rounding has pushed the sum below the minimum value.
Do While numbers.Sum() < 120
    'Get a random element index.
    Dim index = rng.Next(0, numbers.Length)

    'Increment the element at that index.
    numbers(index) = numbers(index) + 1
Loop

'Adjust as required if rounding has pushed the sum above the maximum value.
Do While numbers.Sum() > 235
    'Get a random element index.
    Dim index = rng.Next(0, numbers.Length)

    'Decrement the element at that index.
    numbers(index) = numbers(index) - 1
Loop

'The numbers array now contains six random values with a sum in the range 120 to 235.