【问题标题】:Generate random number to 2 decimal places生成小数点后 2 位的随机数
【发布时间】:2013-06-16 14:37:54
【问题描述】:
blah = CInt(Int((7 * Rnd()) + 0))

生成一个介于 0 和 6 之间的随机整数。

如何修改它以给我一个小数点后 2 位的随机数,仍然在 0 到 6 之间?

如下所示,我现在正在使用此代码,它似乎可以工作:

Dim prng As New Random

Private Function aRand() As Double
    Return Math.Round(prng.Next(0, 601) / 100, 2)
End Function

currentApp.statements(Pick, 7) = aRand()
currentApp.statements(Pick, 8) = aRand()

感谢所有建议。

【问题讨论】:

  • 您的意思是介于 0.00 和 6.99 之间吗?
  • 0.00 和 6.00 之间
  • Steve,我不擅长将 C# 代码转换为 VB.Net。
  • 生成一个介于 0 到 600 之间的数字。除以 100。当然最好避免除以,浮点数并不精确。

标签: vb.net


【解决方案1】:

这样

Dim prng As New Random

Private Function aRand() As Double
    Return prng.Next(0, 601) / 100
End Function

注意随机的位置。

你的代码看起来像

    currentApp.statements(Pick, 7) = aRand()
    currentApp.statements(Pick, 8) = aRand()

【讨论】:

  • 您的答案不会在其范围内包含 6.00。
  • 没错,那个专属上限...我会修复它。
  • 我认为您在编辑后不需要RoundReturn prng.Next(0, 601) / 100 应该可以正常工作。
【解决方案2】:

OP说between 0.00 and 6.00,所以我相信@HansPassant的建议是最好的尝试,但将上限扩大到601(如果他的意思是限制当然包括在内)

Dim rnd As New Random
Dim x As Integer = rnd.Next(0, 601)
Dim d = x / 100
Console.WriteLine(d)

【讨论】:

    【解决方案3】:

    根据@Steve 的回答,这是一个通用实现:

    Function RandomDouble(maxValue As Integer, precision As Integer) As Double
      Static rnd As New Random
      Dim scale As Double = 10 ^ precision
      Dim x As Integer = rnd.Next(0, maxValue * scale + 1)
      Return x / scale
    End Function
    

    以及用法(在一个空的控制台应用程序中测试,子主):

    Dim dbl As Double = RandomDouble(6, 2)
    Debug.WriteLine(dbl)
    

    所以你可以像这样重复使用它:

    currentApp.statements(Pick, 7) = RandomDouble(6, 2)
    currentApp.statements(Pick, 8) = RandomDouble(6, 2)
    

    或者DRY principle (=don't repeat yourself):

    For i As Integer = 7 To 8
      currentApp.statements(Pick, i) = RandomDouble(6, 2)
    Next
    

    【讨论】:

    • 这如何工作?通用的..如果 Random.Next() 只接受 Integers 并且比例是 Double 是的,我测试过它,它会引发错误 'minValue' cannot be greater than maxValue. Parameter name: minValue
    • @SSpoke:你使用了哪些参数?你想达到什么结果?
    • .NET Framework 中标准的 Random 库的 Next() 只接受整数,而 scale 是 Double 不能说除此之外的任何其他内容..我修复了它..我会粘贴你我的代码。 pastebin.com/scNZnM6m我需要一个随机小数点值,小数点前有一个静态值。
    • @SSpoke:将Dim rnd 更改为Static rnd 后(感谢您注意到一个问题,否则它会在所有通道中生成相同的数据),并给定参数(maxValue=6,precision=2) ,它会生成3.72, 2.09, 5.82, 1.71, 0.7, 0.31, 4.63, 5.4, 3.95, 3.43, 1.75,这对我来说是正确的。如果您需要小数点前的常数值,则需要使用我的函数和参数 (maxValue=1,precision=?) 并添加到您的固定值。无需为此编写单独的函数。
    【解决方案4】:

    我想我会在这个问题上大吃一惊,因为我刚刚扩展了@dbasnett 的答案,以制作一个不错的通用生成器。

    注意:我使用了 Decimal 但 Double 可以替换为输出,没有任何问题。

    ''' <summary>
    ''' Random Decimal generator with variable precision"
    ''' </summary>
    ''' <param name="L">Minimum Value</param>
    ''' <param name="U">Maximum Value</param>
    ''' <param name="P">Precision</param>
    ''' <returns>Decimal</returns>
    ''' <remarks></remarks>
    Private Function DRandom(L As Integer, U As Integer, P As Integer) As Decimal
        Dim Rand As New Random
        Dim Upper As String = U.ToString
        Dim Precision As String = "1"
        For I = 0 To P
            If I > 0 Then
                If I = P Then
                    Upper = Upper + "1"
                Else
                    Upper = Upper + "0"
                End If
                Precision = Precision + "0"
            End If
        Next
        Return Rand.Next(L, Upper.toInteger) / Precision.toInteger
    End Function
    

    使用我的通用 toInteger 扩展:

    ''' <summary>
    ''' Handles conversion of variable to Integer.
    ''' </summary>
    ''' <param name="X"></param>
    ''' <param name="I">Returned if conversion fails.</param>
    ''' <returns>Signed 32bit Integer</returns>
    ''' <remarks></remarks>
    <Runtime.CompilerServices.Extension()> _
    Public Function toInteger(Of T)(ByRef X As T, Optional I As Integer = 0) As Integer
        Dim S As String = X.ToString
        Try
            If S = String.Empty Then
                Return I
            Else
                Return Integer.Parse(S)
            End If
        Catch
            Dim result As String = String.Empty
            Dim ReturnInt As Integer
            Dim Parsed As Byte
            For Each Character In S.ToCharArray
                If Character = "-" Then
                    If S.Substring(0, 1).ToString <> "-" Then
                        result = Character + result
                    End If
                End If
                If Character = "." Then
                    Exit For
                End If
                If Byte.TryParse(Character, Parsed) Then
                    result = result + Parsed.ToString
                End If
            Next
            If result <> String.Empty Then
                If Integer.TryParse(result, ReturnInt) Then
                    Return Integer.Parse(ReturnInt)
                Else
                    If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then
                        Return Integer.MaxValue
                    ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then
                        Return Integer.MinValue
                    Else
                        Return I
                    End If
                End If
            Else
                Return I
            End If
        End Try
    End Function
    

    此生成器采用可变精度,并允许您在代码中选择上限和下限允许(在我看来)随机数生成器的最大可重用性。

    希望这对人们有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多