如何在VB中创建非重复随机数

问题描述 投票:-2回答:2

我在VB中创建一个简单的游戏,因为玩家需要一个镜头,这是一个随机的机会它会错过,因为每个点都拍摄了多个镜头,当“Sub playShot()”重复时,我新建一个新的随机数,数字需要在1到10之间。

这是我目前拥有的,但它只输出相同的数字:

Dim rnd As New Random
Dim shot As Integer = rnd.Next(1, 11)

我很好,如果它由于机会重复这个数字,但目前它总是一样的。

vb.net
2个回答
1
投票

碰巧我现在有一个项目打开,其中包含用于随机化对象列表的扩展方法:

Imports System.Runtime.CompilerServices

''' <summary>
''' Contains methods that extend the <see cref="IEnumerable(Of T)"/> interface.
''' </summary>
Public Module EnumerableExtensions

    ''' <summary>
    ''' A random number generator.
    ''' </summary>
    Private rng As Random

    ''' <summary>
    ''' Randomises the items in an enumerable list.
    ''' </summary>
    ''' <typeparam name="T">
    ''' The type of the items in the list.
    ''' </typeparam>
    ''' <param name="source">
    ''' The input list.
    ''' </param>
    ''' <returns>
    ''' The items from the input list in random order.
    ''' </returns>
    <Extension>
    Public Function Randomize(Of T)(source As IEnumerable(Of T)) As IEnumerable(Of T)
        EnsureRandomInitialized()

        Return source.Randomize(rng)
    End Function

    ''' <summary>
    ''' Randomises the items in an enumerable list.
    ''' </summary>
    ''' <typeparam name="T">
    ''' The type of the items in the list.
    ''' </typeparam>
    ''' <param name="source">
    ''' The input list.
    ''' </param>
    ''' <param name="random">
    ''' A random number generator.
    ''' </param>
    ''' <returns>
    ''' The items from the input list in random order.
    ''' </returns>
    <Extension>
    Public Function Randomize(Of T)(source As IEnumerable(Of T), random As Random) As IEnumerable(Of T)
        Return source.OrderBy(Function(o) random.NextDouble())
    End Function

    ''' <summary>
    ''' Initialises the random number generator if it is not already.
    ''' </summary>
    Private Sub EnsureRandomInitialized()
        rng = If(rng, New Random)
    End Sub

End Module

在项目中使用该模块,您可以执行以下操作:

Dim numbers = Enumerable.Range(1, 10).Randomize()

For Each number In numbers
    Console.WriteLine(number)
Next

该模块包含两个重载,第二个允许您根据需要传入自己的Random实例。如果您只想使用第一个过载,那么模块可以稍微简化:

Imports System.Runtime.CompilerServices

''' <summary>
''' Contains methods that extend the <see cref="IEnumerable(Of T)"/> interface.
''' </summary>
Public Module EnumerableExtensions

    ''' <summary>
    ''' A random number generator.
    ''' </summary>
    Private rng As Random

    ''' <summary>
    ''' Randomises the items in an enumerable list.
    ''' </summary>
    ''' <typeparam name="T">
    ''' The type of the items in the list.
    ''' </typeparam>
    ''' <param name="source">
    ''' The input list.
    ''' </param>
    ''' <returns>
    ''' The items from the input list in random order.
    ''' </returns>
    <Extension>
    Public Function Randomize(Of T)(source As IEnumerable(Of T)) As IEnumerable(Of T)
        rng = If(rng, New Random)

        Return source.OrderBy(Function(o) rng.NextDouble())
    End Function

End Module

0
投票

当您有一个不希望重复随机值的范围时,通常的解决方案是按顺序生成范围内的所有数字(在本例中为1到10)。然后你洗牌顺序。现在,您可以遍历您的混洗数据。

© www.soinside.com 2019 - 2024. All rights reserved.