无法理解随机的逻辑。 [英] Unable to understand the logic of Random.

查看:71
本文介绍了无法理解随机的逻辑。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我写了以下代码在控制台上打印随机数。

  class 计划
{

static void Main( string [] args)
{

Console.WriteLine(randomNo( ));
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());


Console.ReadLine();
}

private static int randomNo()
{
Random r = new Random();
int n = r.Next( 56 );
return n;
}





编译此代码的那一刻,它实际上重复相同的数字。我认为它会产生像(4,5,6,9,8,9,6)那样的随机数,但事实并非如此。我查看了互联网上的几个资源,然后才知道

随机r =  Random()



必须在类级别定义,并且运行良好。但我无法弄清楚为什么需要在班级定义?



什么是种子?



请帮我解决这个问题。

解决方案

试试这个



< pre lang =cs> class 程序
{
static void Main( string [] args)
{

Console.WriteLine( randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());


Console.ReadLine();
}
静态随机r = new Random(); // 在外面声明..
private static int randomNo()
{
< span class =code-keyword> int n = r.Next( 56 );
return n;
}
}





查看这些

随机 [ ^ ]

c#随机 [ ^ ]


随机数生成从种子值开始。如果重复使用相同的种子,则生成相同的数字序列。产生不同序列的一种方法是使种子值与时间相关,从而与每个新的Random实例产生不同的序列。默认情况下,Random类的无参数构造函数使用系统时钟生成其种子值,而其参数化构造函数可以根据当前时间中的滴答数采用Int32值。但是,由于时钟具有有限的分辨率,因此使用无参数构造函数以紧密连续的方式创建不同的随机对象会创建随机数生成器,从而生成相同的随机数序列。



Seed是一个用于计算伪随机数序列起始值的数字。如果指定了负数,则使用该数字的绝对值。





 使用系统; 
使用 System.Data.SqlClient;

命名空间测试
{
class Program
{
私有 静态随机r = new 随机( 25 );
静态 void Main( string [] args)
{

Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());
Console.WriteLine(randomNo());


Console.ReadLine();
}

private static int randomNo()
{
int n = r.Next( 56 );
return n;
}
}


}


尝试此链接以获取有关随机的更多信息生成数字



http://csharpindepth.com/Articles/第12章/ Random.aspx [ ^ ]



和另一个链接是。

http://stackoverflow.com/questions/13539974/random-number-generator-c-sharp [ ^ ]


Hi,

I have written the following code to print random numbers on the console.

class Program
    {
        
        static void Main(string[] args)
        {
            
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());


            Console.ReadLine();
        }

        private static int randomNo()
        {
            Random r = new Random();
            int n = r.Next(56);
            return n;
        }



The moment i compile this code, it actually repeating the same numbers . I thought that it will generate random numbers like (4,5,6,9,8,9,6) but its not. I looked over several resources on internet and came to know that

Random r = new Random()


must be defined on the class level and it worked well. But i am not able to figure out why is it needed to define at class level?

And what are Seed?

Please help me out to get this logic.

解决方案

try this

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());


        Console.ReadLine();
    }
   static Random r = new Random();  // declare it outside..
    private static int randomNo()
    {
        int n = r.Next(56);
        return n;
    }
}



Check these
Random[^]
c# Random[^]


The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. 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.

Seed is a number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.


using System;
using System.Data.SqlClient;

namespace Test
{
    class Program
    {
       private static Random r = new Random(25);
        static void Main(string[] args)
        {

            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());


            Console.ReadLine();
        }

        private static int randomNo()
        {
            int n = r.Next(56);
            return n;
        }
    }


}


try this link for more information about the random Number Generate

http://csharpindepth.com/Articles/Chapter12/Random.aspx[^]

and another link is.
http://stackoverflow.com/questions/13539974/random-number-generator-c-sharp[^]


这篇关于无法理解随机的逻辑。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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