如何创建2个随机值

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

我写了

但是dieOne和dieTwo总是相等的,您如何制作两个独立的随机值? Mikanu ,现在可以使用了.

在2009年7月13日星期一10:43 PM修改

解决方案

您应创建一个Random实例作为类的静态字段,并在需要时使用它.它永远不应是方法的局部变量.

正确的方法是创建 Random 类的实例,然后调用以下方法之一:-Next(Int32 max) // will return a random number smaller than max -NextByte() // will return a random byte (between 0 and 255) -NextDouble() // will return a random number between 0.0 and 1.0<br /> 这是一个代码示例,它将生成介于0和10之间的两个随机数a和b

<br />Random rnd = new Random();<br />int a = rnd.Next(10);<br />int b = rng.Next(10);<br />

I wrote

<br /> Random RollOne = new Random();<br /> Random RollTwo = new Random();<br /> dieOne = RollOne.Next(1, 7);<br /> dieTwo = RollTwo.Next(1 , 7);<br /> sum = dieOne + dieTwo;<br />

but dieOne and dieTwo are always equal, how do you make two independent random values?Edit:Thanks Mikanu, it works now.

modified on Monday, July 13, 2009 10:43 PM

解决方案

You should create one instance of Random as a static field of the class and use it whenever you need it.It should never be a local variable of a method.

The correct way to do that is to create an instance of the Random class and then call one of the following methods: - Next(Int32 max) // will return a random number smaller than max - NextByte() // will return a random byte (between 0 and 255) - NextDouble() // will return a random number between 0.0 and 1.0<br />Here''s a code sample which will generate two random numbers, a and b, between 0 and 10

<br />Random rnd = new Random();<br />int a = rnd.Next(10);<br />int b = rng.Next(10);<br />

更多推荐

如何创建2个随机值

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

发布评论

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

>www.elefans.com

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