C#随机复数为(实数,虚数)在100,200之间? [英] C# random complex number for (real, imaginary number) between 100 , 200?

查看:102
本文介绍了C#随机复数为(实数,虚数)在100,200之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在C#中的代码,但它生成相同数量的特定金额,如下所示:

180 + 152 i

180 + 152 i

180 + 152 i

180 + 152 i

180 + 152 i





任何想法?



代码:



Here is my code in C# , but it generate same number for specific amount number like this:
180 + 152 i
180 + 152 i
180 + 152 i
180 + 152 i
180 + 152 i


any idea?

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ex2
{
    class Complex
    {
        public double real, imag;
        public void Set()
        {
        

            Random r = new Random();
            imag = r.Next(100,200);
            real = r.Next(100,200);
   
        }

        public void Write()
        {
            char sign;
            sign = (imag > 0) ? '+' : '-';
            Console.WriteLine("{0} {1} {2} i ", real, sign, Math.Abs(imag));
        }

    }
    class Program
    {
        private static int n;
        static void Main(string[] args)
        {
            Console.Write("How Many Number : ");
            n = Convert.ToInt32(Console.ReadLine());

            Complex[] complexArray = new Complex[n];
            Random r = new Random();
           

            for (int i= 0; i < n; ++i)
            {
                complexArray[i] = new Complex();
            }

           
            for (int i = 0; i < n; ++i)
            {
                complexArray[i].Set();
                complexArray[i].Write();
            }

            realSort(complexArray);

            Console.WriteLine();

            for (int i = 0; i < n; ++i)
                complexArray[i].Write();

            Console.ReadKey();
        }

        public static void realSort(Complex[] s)
        {
            Complex temp;
            for (int i = 1; i <= s.Length - 1; ++i)
                for (int j = 0; j < s.Length - i; ++j)
                    if (s[j].real > s[j + 1].real)
                    {
                        temp = s[j];
                        s[j] = s[j + 1];
                        s[j + 1] = temp;
                    }
        }
    }
}

推荐答案

PieBaldConsult正确地指出了 key 问题在这里:



每当你想要一对随机数时,你实例化一个'随机对象的新实例。



当您实例化没有种子参数的随机对象时,它每次都会给你相同的结果。



所以,你需要创建一个'Random对象的实例,然后重新使用它。



我认为正如PieBaldConsult建议的那样做是好的做法你的随机对象声明'静态,并根据每次应用程序启动时会有所不同的内容给它一个初始的种子值:
PieBaldConsult correctly pointed out the key problem here:

You instantiate a new instance of the 'Random object each time you want a pair of random numbers.

When you instantiate the 'Random object with no "seed" argument, it will give you the same results every time.

So, you need to create one instance of the 'Random object, and re-use it.

I think it's good practice, as PieBaldConsult suggested, to make your Random object declaration 'static, and give it an initial "seed" value based on something that will vary every time the Application starts:
private static Random TheRandom = new Random(DateTime.Now.Millisecond);
private static double rndMin = 100.0;
private static double rndRangeIncrement = 100.0;

private double rndReal, rndImag;

你将'real和'imag变量声明为双精度,所以问题出现了为什么你使用随机生成的整数?



假设您确实希望双精度映射到范围100.0~200.0:

You declare the 'real and 'imag variables as doubles, so the question comes up why you are using randomly generated integers ?

Assuming you do want doubles mapped to the range 100.0~200.0:

rndReal = (TheRandom.NextDouble() * rndMin) + rndRangeIncrement;
rndImag = (TheRandom.NextDouble() * rndMin) + rndRangeIncrement;

Console.WriteLine("real: {0} imaginary: {1}{2}", rndReal, rndImag, Environment.NewLine);

您的代码中还有其他地方让我困惑:您的'Write方法意味着您的随机数可能是负数。



希望了解这里显示的技术将帮助您按照预期的方式运行其余的代码。

There are other places in your code that puzzle me: your 'Write method implies that your random numbers can be negative.

Hopefully, understanding the techniques shown here will help you get the rest of your code working the way you intend.


这是因为你只发生了对于 imag real ,只执行两次随机数生成。只需在您自己的代码中执行文本搜索就可以了。更一般地说,代码充满了废话。例如,您在 Complex Program Random 的两个实例$ c>,永远不要使用第二个。难怪你遇到一些问题... :-)



-SA
It happens because you only perform random number generation only twice, for imag and real. It was enough to perform a text search in your own code, to see it. More generally, the code is full of nonsense. For example, you create two instances of Random, in Complex and Program, and never use the second one. No wonder you get some problems… :-)

—SA


这篇关于C#随机复数为(实数,虚数)在100,200之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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