Vb.Net:如何生成随机nuber,不包括previos generaded? [英] Vb.Net: how to generate random nuber, not including previos generaded?

查看:146
本文介绍了Vb.Net:如何生成随机nuber,不包括previos generaded?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vb.Net:如何生成随机nuber,不包括previos generaded ???

Vb.Net: how to generate random nuber, not including previos generaded???

推荐答案

这样做的唯一方法是:

1)跟踪您之前使用过的值,如果在列表中找到随机值,则生成另一个值



2)设置一个列出所有可能的值并随机生成索引。将该索引处的值设置为随机数并将其从列表中删除。



它们都有优点和缺点:



随着使用值的增加,第一种方法可能会非常慢 - 因为每次生成一个已使用的新随机数的机会更多。在极端情况下,它可能永远不会找到未使用的值。

如果您有多种数字,第二种方法在内存方面效率非常低。但是对于有限的范围,它比其他方法更具确定性。
The only way to do that is to either :
1) Keep track of the values you have previously used and generate another if you find a random value in the list
Or
2) Set up a List of all possible values and generate an index randomly. Set the value at that index to be the random number and remove it from the list.

They both have advantages and disadvantages:

The first method can be extremely slow as the number of "used" values increases - because there is more chance each time of generating a new random number that has been used. In extreme cases, it may never find an unused value.
The second method can be very inefficient in memory terms if you have a wide range of numbers. For limited ranges however, it is a lot more deterministic than the other method.


我制作了一个宾果游戏,并为每个新游戏使用随机生成的75个球数。



数组是公共的,是aryRnd(75)。这对我来说效果很好而且速度相当快。



然后你可以根据需要在项目中使用数组。



I made a Bingo game and use a randomly generated set of 75 ball numbers for each new game.

The array is public and is aryRnd(75). This works well for me and is reasonably fast.

You can then use the array as needed in the project.

Dim valueRnd As String = ""
Dim ballCnt As Single = 0
Dim gotcha As Boolean = False
Dim z As Single = 0
Dim Xy As Single

For Xy = 0 To 75    'Function BallList
    aryRnd(Xy) = 0
Next (Xy)

Do While ballCnt <= 74
    gotcha = False

    ' Initialize the random-number generator.
    Randomize()

    '      Generate random value between 1 and 75.
    valueRnd = CInt(Int((75 * Rnd()) + 1))

    '    Check for existence of the number in the array
    For Xy = 1 To ballCnt + 1
        If aryRnd(Xy) = valueRnd Then
            gotcha = True
            Exit For
        End If
    Next Xy

    '   Add to array if not found
    If Not gotcha Then
        aryRnd(ballCnt + 1) = valueRnd
        ballCnt = ballCnt + 1
    End If
Loop


这篇关于Vb.Net:如何生成随机nuber,不包括previos generaded?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆