偶然的遭遇不是这样随意 [英] Random encounter not so random

查看:140
本文介绍了偶然的遭遇不是这样随意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一些问题的产生与C#随机数
现在我有这个功能。

Hello i am having some problems generating random numbers with C# Now i have this function.

public Color getRandomColor()
{
    Color1 = new Random().Next(new Random().Next(0, 100), new Random().Next(200, 255));
    Color2 = new Random().Next(new Random().Next(0, 100), new Random().Next(200, 255));
    Color3 = new Random().Next(new Random().Next(0, 100), new Random().Next(200, 255));
    Color color = Color.FromArgb(Color1, Color2, Color3);
    Console.WriteLine("R: " + Color1 + " G: " + Color2 + " B: " + Color3 + " = " + color.Name);
    return color;
}

现在,你可能会注意到,有新的随机的ALOT()那里,那是因为我想淘汰,这可能是一个相同的实例错误的概率。

Now you might notice that there are ALOT of new Random() there, that is because i want to weed out the probability that it could be a same instance error.

我现在运行这个函数8次,几次。
现在,这里是出看跌期权。

I now run this function 8 times, a couple of times. Now here are the out puts.

R: 65 G: 65 B: 65 = ff414141
R: 242 G: 242 B: 242 = fff2f2f2
R: 205 G: 205 B: 205 = ffcdcdcd
R: 40 G: 40 B: 40 = ff282828
R: 249 G: 249 B: 249 = fff9f9f9
R: 249 G: 249 B: 249 = fff9f9f9
R: 94 G: 94 B: 94 = ff5e5e5e
R: 186 G: 186 B: 186 = ffbababa

R: 142 G: 142 B: 142 = ff8e8e8e
R: 190 G: 190 B: 190 = ffbebebe
R: 19 G: 19 B: 19 = ff131313
R: 119 G: 119 B: 119 = ff777777
R: 119 G: 119 B: 119 = ff777777
R: 75 G: 75 B: 75 = ff4b4b4b
R: 169 G: 169 B: 169 = ffa9a9a9
R: 127 G: 127 B: 127 = ff7f7f7f

R: 73 G: 73 B: 73 = ff494949
R: 27 G: 27 B: 27 = ff1b1b1b
R: 125 G: 125 B: 125 = ff7d7d7d
R: 212 G: 212 B: 212 = ffd4d4d4
R: 174 G: 174 B: 174 = ffaeaeae
R: 0 G: 0 B: 0 = ff000000
R: 0 G: 0 B: 0 = ff000000
R: 220 G: 220 B: 220 = ffdcdcdc

正如你可以看到这一次也不是那么随意,但为什么剂量出现这种情况?以及如何应对呢?

As you can see this is not so random again, but why dose this happens? and how can i counter it?

推荐答案

您正在创建一个新的随机您需要的每个单独的值。

You're creating a new Random for each single value you need.

尝试创建一个独特的随机对象并调用。接下来()函数多次。

Try creating a unique Random object and calling the .Next() function multiple times.

public Color getRandomColor()
{
    Random rand = new Random();

    Color1 = rand.Next(rand.Next(0, 100), rand.Next(200, 255));
    Color2 = rand.Next(rand.Next(0, 100), rand.Next(200, 255));
    Color3 = rand.Next(rand.Next(0, 100), rand.Next(200, 255));
    Color color = Color.FromArgb(Color1, Color2, Color3);
    Console.WriteLine("R: " + Color1 + " G: " + Color2 + " B: " + Color3 + " = " + color.Name);
    return color;
}

拍摄文档随机对象:

默认情况下,Random类的参数的构造函数使用系统时钟产生的种子值,而其参数的构造函数可以根据当前时间的刻度数的Int32值。但是,由于时钟具有有限的分辨率,使用参数的构造函数来创建紧密相继不同的随机对象的创建产生的随机数相同序列的随机数生成器

By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers

这篇关于偶然的遭遇不是这样随意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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