随机数但不重复

发布于 2024-07-30 02:47:06 字数 62 浏览 5 评论 0原文

我想生成一个小于 50 的随机数,但一旦生成该数字,我希望它不能再次生成。

谢谢您的帮助!

I would like to generate a random number less than 50, but once that number has been generated I would like it so that it cannot be generated again.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

药祭#氼 2024-08-06 02:47:06

请参阅:Fisher-Yates 洗牌

public static void shuffle (int[] array) 
{
    Random rng = new Random();       // i.e., java.util.Random.
    int n = array.length;            // The number of items left to shuffle (loop invariant).
    while (n > 1) 
    {
        n--;                         // n is now the last pertinent index
        int k = rng.nextInt(n + 1);  // 0 <= k <= n.
        int tmp = array[k];
        array[k] = array[n];
        array[n] = tmp;
    }
}

Please see: Fisher–Yates shuffle:

public static void shuffle (int[] array) 
{
    Random rng = new Random();       // i.e., java.util.Random.
    int n = array.length;            // The number of items left to shuffle (loop invariant).
    while (n > 1) 
    {
        n--;                         // n is now the last pertinent index
        int k = rng.nextInt(n + 1);  // 0 <= k <= n.
        int tmp = array[k];
        array[k] = array[n];
        array[n] = tmp;
    }
}
無處可尋 2024-08-06 02:47:06

将数字1-49放入可排序集合中,然后按随机顺序排序; 根据需要从集合中弹出每一个。

Put the numbers 1-49 in a sortable collection, then sort it in random order; pop each one out of the collection as needed.

南渊 2024-08-06 02:47:06

鉴于问题被标记为 VB/VB.Net...这是 Mitch 答案的 VB 实现。

Public Class Utils

   Public Shared Sub ShuffleArray(ByVal items() As Integer)

      Dim ptr As Integer
      Dim alt As Integer
      Dim tmp As Integer
      Dim rnd As New Random()

      ptr = items.Length

      Do While ptr > 1
         ptr -= 1
         alt = rnd.Next(ptr - 1)
         tmp = items(alt)
         items(alt) = items(ptr)
         items(ptr) = tmp
      Loop

   End Sub

End Class

Seeing as the question was tagged VB/VB.Net... this is a VB implementation of Mitch's answer.

Public Class Utils

   Public Shared Sub ShuffleArray(ByVal items() As Integer)

      Dim ptr As Integer
      Dim alt As Integer
      Dim tmp As Integer
      Dim rnd As New Random()

      ptr = items.Length

      Do While ptr > 1
         ptr -= 1
         alt = rnd.Next(ptr - 1)
         tmp = items(alt)
         items(alt) = items(ptr)
         items(ptr) = tmp
      Loop

   End Sub

End Class
靖瑶 2024-08-06 02:47:06

下面的代码生成字母数字字符串,其长度作为参数传递。

Public Shared Function GetRandomAlphaNumericString(ByVal intStringLength As Integer) As String

Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Dim intLength As Integer = intStringLength - 1

Dim stringChars = New Char(intLength) {}

Dim random = New Random()

    For i As Integer = 0 To stringChars.Length - 1
        stringChars(i) = chars(random.[Next](chars.Length))
    Next

    Dim finalString = New [String](stringChars)
    Return finalString
End Function

Below code generate alpha-numeric string with length that you pass as parameter.

Public Shared Function GetRandomAlphaNumericString(ByVal intStringLength As Integer) As String

Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Dim intLength As Integer = intStringLength - 1

Dim stringChars = New Char(intLength) {}

Dim random = New Random()

    For i As Integer = 0 To stringChars.Length - 1
        stringChars(i) = chars(random.[Next](chars.Length))
    Next

    Dim finalString = New [String](stringChars)
    Return finalString
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文