如何循环构造函数以生成不同的值? C# [英] How to loop constructor to produce different values? c#

查看:119
本文介绍了如何循环构造函数以生成不同的值? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码的新手,只是在研究一个小项目。



我正在尝试生成多个不同版本的属性集我将插入一个SQLite数据库。



我目前的代码每次打印相同的数字,即:



  7  
2
12
13
7
2
12
13



...等



我猜它有与构造函数循环有关?无论如何我无法弄清楚它为什么会发生或如何解决它,任何帮助或解释都将不胜感激。



谢谢!



我尝试了什么:



  for  int  i =  0 ; i <   10 ; i ++)
{
GenData gd = new GenData();
gd.GenStats();
}

// 上面的方法在不同的类到下面。

class GenData
{

public void GenStats()
{

Random rand = new Random();

int [] arr = new int [ 4 ];

for int x = 0 ; x < arr.Length; x ++)
{

// 加权概率值

如果 (rand.NextDouble()< 0 90
{
arr [x] = rand.Next( 1 15 < /跨度>);
}
else
{
arr [x] = rand.Next( 1 20 );
}
}

foreach var in arr)
{
Console.WriteLine(item.ToString());
}

int sum = arr.Sum();

// Console.WriteLine(Sum:+ sum + Environment.NewLine);
}
}

解决方案

首先,将Random实例移到任何方法之外,然后私有化:

  class  GenData 
{
private Random rand = new Random();
public void GenStats()
{

这样,每次调用方法时都不会重新创建,这可能意味着重复的值。这可能是造成这种情况的原因,因为Random构造函数使用系统时间来启动序列,如果你连续快速调用GenStats例程,构造函数调用之间的时间不会改变,因为你的处理器太快了!

其次,如果您要如此快速地构建新的GenData实例,那么也不会完全修复它 - 所以让Random实例静态:

< pre lang =c#> class GenData
{
private static 随机rand = new Random();
public void GenStats()
{

通常,我不会建议,因为它不一定是线程安全的,但是对于这个练习它可能没问题。


你需要播种随机生成器,所以它从每次都有不同的价值。使用以毫秒为单位的时钟时间是一种有用的方法。请参阅随机构造函数(Int32)(系统) [ ^ ]。

I'm new to coding, and just working on a little project to learn.

I'm trying to produce multiple different versions of sets of attributes that I'll be inserting into a SQLite database.

My current code prints the same numbers each time, ie:

7
2
12
13
7
2
12
13


... etc.

I guess it has to do with the constructor loop? Anyway I can't figure out why it is happening or how to solve it, any help or explanation would be greatly appreciated.

Thanks!

What I have tried:

            for (int i = 0; i < 10; i++)
            {
                GenData gd = new GenData();
                gd.GenStats();
            }

//Above in method in different class to below.

    class GenData
    {
        
        public void GenStats()
        {

            Random rand = new Random();

            int[] arr = new int[4];

            for (int x = 0; x < arr.Length; x++)
            {

//Weighted probability of values

                if (rand.NextDouble() < 0.90)
                {
                    arr[x] = rand.Next(1, 15);
                }
                else
                {
                    arr[x] = rand.Next(1, 20);
                }
            }

            foreach (var item in arr)
                {
                Console.WriteLine(item.ToString());
                }

            int sum = arr.Sum();

            //Console.WriteLine("Sum: " + sum + Environment.NewLine);
        }
    }

解决方案

First off, move the Random instance outside any method, and make it private:

class GenData
{
    private Random rand = new Random();
    public void GenStats()
    {

That way, it isn't recreated each time you call the method, which can mean repeated values. That's probably what's causing the problem in this case, because the Random constructor uses the system time to "start the sequence" and if you call the GenStats routine quickly in succession, the time doesn't change between constructor calls because your processor is too fast!
Secondly, if you are going to build new GenData instances so quickly, that isn;t going to fix it completely either - so make the Random instance static:

class GenData
{
    private static Random rand = new Random();
    public void GenStats()
    {

Normally, I wouldn't suggest that as it isn't necessarily thread safe, but for this exercise it's probably fine.


You need to seed the random generator so it starts at a different value each time. Using the clock time in milliseconds is a useful way of doing it. See Random Constructor (Int32) (System)[^].


这篇关于如何循环构造函数以生成不同的值? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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