随机,随机化,Rnd,种子值 [英] Random, Randomize, Rnd, seed values
问题描述
嘿所有,
我一直想知道在一定范围内生成随机数一段时间。
最近我我一直在使用这样的东西:
Hey all,
I''ve been wondering about generating random numbers within a certain range for a while.
Recently I''ve been using something like this a lot:
Dim Generator As New Random
Generator.Next(1, 30)
(生成1的数字)最多包括29)
过去我被告知使用Rnd并且我需要先调用Randomize,但因为我可以轻松生成一个随机数随机的一个范围我一直在使用它。很久以前我用过Rnd和Randomize。
我想做的是这样的:
我想要的当我加载表格时洗牌2副牌。
要洗牌一个牌组,我这样做:
(this generates numbers ranging from 1 up to and including 29)
In the past I was told to use Rnd and that I needed to call Randomize first, but because I can easily generate a random number within a range with Random I''ve been using that. It was long ago that I used Rnd and Randomize.
What I''m trying to do is something like this:
I want to shuffle 2 decks of cards as I load a form.
To shuffle one deck I do this:
Dim DeckBefore As New Collection
Dim DeckAfter As New Collection
Dim Position As New Random
Dim ChosenCard As Integer
While DeckBefore.Count > 0
ChosenCard = Position.Next(1, DeckBefore.Count + 1)
DeckAfter.Add(DeckBefore.Item(ChosenCard))
DeckBefore.Remove(ChosenCard)
End While
想象一下Deck之前要成为这样的卡片列表:Spaces的空间,黑桃的两个,黑桃的三个......等等。
DeckAfter可能是这样的:Jack of Hearts,Seven俱乐部,钻石之王,黑桃之王......等等
我遇到的问题是,如果我将这个代码用于2个单独的套牌,他们最终会相同(虽然每次都不一样)。我知道这与Random的种子值有关,如果我不给它一个,它会自动选择种子值的日期/时间。
我想要的只是我的程序每次随机订购卡片,以便玩家无法预测(从他/她的头顶出来)下一张卡片。
任何帮助都将受到赞赏。
Imagine DeckBefore to be a list of cards like this: Ace of Spaces, Two of Spades, Three of Spades .. and so on
DeckAfter could be something like this: Jack of Hearts, Seven of Clubs, Three of Diamonds, King of Spades .. and so on
The problem I encounter is that if I use this code for 2 separate decks, they end up identically (although not the same each time). I understand this has to do with the seed value of the Random, and that is automatically picks the date/time for a seed value if I don''t give it one.
All I want really is for my program to order cards "randomly" each time so that a player won''t be able to predict (out of the top of his/her head) what card comes next.
any help would be appreciated.
推荐答案
不要在方法中声明Random对象。尝试在类级别声明一次。
Don''t declare the Random object in the method. Try declaring it once at the class level.
如果您愿意,可以选择自己设计的种子,但计算机上没有随机序列。我认为你还是安全的,这就是任何卡计划可能做的所有事情。
You can choose a seed of your own devising if you like, but there is no such thing as a random sequence on a computer. I think you''re still safe, this is about all that any card program probably does.
这篇关于随机,随机化,Rnd,种子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!