如何获得真正的随机数组 [英] How to Earn A Real Random Array

查看:130
本文介绍了如何获得真正的随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家



查看我的代码:

Hello Experts

See my code:

char[] elementArray = new char[] { 'A', 'B', 'C', 'D' };

char[] mainArray = new char[3000];

Random random;
for (int i = 0; i < 3000; i++)
{
    int seed = (int)DateTime.Now.Ticks;
    random = new Random(seed);
    mainArray[i] = elementArray[random.Next(4)];
}



这个简单的代码生成我的mainArray;但不完全是我想要的。



例外结果样本:


This simple code generate my mainArray; But not exactly what I want.

Sample of Excepted results:

A B D A A C B C D A B B C A D A C C D A C D B B B B A C...



我通常从以上代码中获得的收益:


What I earn from the above code usually:

A C C C C C C C C C ... A A A A A A A ... D D D D D D ...





我如何才能获得正确的结果?



如果现在还不清楚让我知道!



How can I earn the correct result?

If it is not clear let me know!

推荐答案

你的错误很常见:你需要创建一个随机在应用程序域的生命周期中只有一次,不是在循环中,而是在它之前。在循环之外的循环内移动两个第一行。在它之前。



-SA
Your mistake is very common: you need to create an instance of Random only once in the lifetime of your application domain, not in the loop, but before it. Move two first lines inside loop outside of the loop, before it.

—SA


这篇关于如何获得真正的随机数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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