我如何创建随机数?

编程入门 行业动态 更新时间:2024-10-27 08:34:57
本文介绍了我如何创建随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何创建4位随机数字?不使用结构循环和结构条件(例如Loop for,while,if,...)并且不使用Timer 此程序的结果如此程序: 4位数生成必须不同,例如,如果返回1111为假。但结果1234是真的。

How can I create 4-digit Random Numbers? without use Structural Loops and Structural condition (for example Loop for,while,if,...) and without use from Timer result of this program like this program: 4-digit Produced Must different for example if return 1111 is false. but result 1234 is true.

static void Main(string[] args) { Random Item = new Random(); int Num1, Num2, Num3, Num4; do { Num1 = Item.Next(0, 10); Num2 = Item.Next(0, 10); Num3 = Item.Next(0, 10); Num4 = Item.Next(0, 10); } while (Num1 == Num2 || Num1 == Num3 || Num1 == Num4 || Num2 == Num3 || Num2 == Num4 || Num3 == Num4); Console.WriteLine("{0}{1}{2}{3}", Num1, Num2, Num3, Num4); Console.ReadKey(); }

推荐答案

请参阅:msdn.microsoft/en-us/library/system.random%28v=vs.110%29.aspx [ ^ ]。 不要犯常见错误,创建 System.Random 的实例几次。记住,算法伪randon 。要随机化,请始终使用种子: msdn.microsoft/en-us/library/ctssatww%28v=vs.110%29.aspx [ ^ ]。 从上面引用的文章中显示的代码示例中可以看到,您可以使用 System.DateTime.Now 进行种子设定。要了解为什么需要播种以及伪随机算法如何为您提供随机数,请参阅: en.wikipedia/ wiki / Pseudorandom_number_generator [ ^ ]。 创建 System.Random 的实例后,重复使用它以满足您的所有需求,只需一个实例。在你的情况下,所有其他调用应该是 System.Random(int,int): msdn.microsoft/en-us/library/2dx6wyd4(v = vs.110)的.aspx [ ^ ]。 警告!参数名称确实具有误导性。 minValue 确实是最小值,但 maxValue 并不是最大值;文档称之为独占上限,这意味着实际最大值为 maxValue - 1 。 :-) 另请参阅返回值部分中的注释。 [END EDIT] 你的无循环简直荒谬。好吧,将调用编码为 Random.Next 100次以获得100个数字,但我怀疑是否有人会这么疯狂。
-SA
Please see: msdn.microsoft/en-us/library/system.random%28v=vs.110%29.aspx[^]. Don't make a common mistake, creating the instance of System.Random several times. Remember, the algorithm pseudo-randon. To randomize of, always use the seed: msdn.microsoft/en-us/library/ctssatww%28v=vs.110%29.aspx[^]. As you can see from the code sample shown in the article referenced above, you can use System.DateTime.Now for seeding. To understand why seeding is needed and how pseudo-random algorithm gives you random numbers, please see: en.wikipedia/wiki/Pseudorandom_number_generator[^]. After you create your instance of System.Random, reuse it for all your needs, just one instance. In your case, all other calls should be to System.Random(int, int): msdn.microsoft/en-us/library/2dx6wyd4(v=vs.110).aspx[^]. Warning! The parameter names are really misleading. minValue is really the minimum, but maxValue is not really maximum; the documentation calls it "exclusive upper bound", which means that real maximum is maxValue − 1. :-) See also the notes in Return value section. [END EDIT] Your "without loops" is simply an absurd. Okay, code the calls to Random.Next 100 times to get 100 numbers, but I doubt anyone would be so insane.
—SA

有类似的东西,你没有任何可见的循环或条件(假设,你想生成一个4位数字): With something like that, you don't have any "visible" loop or condition (assuming, you want to generate a single 4 digits number): class FourRandomDigits { public static string Run() { var generator = new FourRandomDigits(); return generator.DoRun(); } private static Random rand = new Random(); private int[] digits = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private int remainingCount = 10; private string DoRun() { var builder = new StringBuilder(); builder.Append(PickOneDigit()); builder.Append(PickOneDigit()); builder.Append(PickOneDigit()); builder.Append(PickOneDigit()); return builder.ToString(); } private int PickOneDigit() { int index = rand.Next(remainingCount); int result = digits[index]; digits[index] = digits[--remainingCount]; return result; } }

更多推荐

我如何创建随机数?

本文发布于:2023-10-28 23:06:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!