随机类生成相同的数字 [英] Random class generating same number
本文介绍了随机类生成相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
在我的项目中,我使用 Random
类来每次生成随机数在1-9之间,但是它在一定时间内连续生成相同的数字,因为我的项目执行花了很多时间所以请为此提供任何解决方案
代码:
Hi all,
In my project I am using Random
class to generate random numbers every time in between 1-9, but it is generating same number continuously for certain time, because of this my project execution taking lot of time so please provide any solution for this
code:
Random rd = new Random();
Object ranNo = (Object)rd.Next(1, 9);
这里我想使用'ranNo'变量值。
谢谢,
Ram
here I want to use 'ranNo' variable value.
Thanks,
Ram
推荐答案
参考类似解决的QA:
点击此处 [ ^ ]
Refer similar solved QA:
click here[^]
你应该创建随机
对象rd
一次并在每次需要新的随机数时调用其Next
方法。
如果你需要唯一的号码,然后查看我的提示:从牌组中随机抽取5张牌 [ ^ ](我知道是C ++
代码,但是,你知道,算法很重要。)
You should create theRandom
objectrd
once and call itsNext
method every time you need a new random number.
If you need unique numbers then have a look at my tip: "Random extraction of 5 cards from a deck"[^] (I do know isC++
code, but, you know, just the algorithm matters).
这篇关于随机类生成相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文