如何制作随机数组C# [英] How can make random array C#

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

问题描述

大家好

我在C#中有一个数组,如何相互随机替换数组成员?



我尝试过:



for show array我使用Listbox



Hi guys
I have an array in C# and How can I replace the members of the array randomly with each other?

What I have tried:

for show array I use Listbox

inputCount= lstin.Items.Count;
output = UniqueRandom(0, inputCount).ToArray<int>();//Just Make me random Number Without Duplicate
for (int i = 0; i < output.Length; i++)
   { 
       lstout.Items.Add(lstin.Items[output[i]]);
   }

推荐答案

你的意思是随机播放 [ ^ ]?


最简单的解决方案:

Simplest solution:
private Random shuffle = new Random();
public void Randomise<T>(List<T> input)
    {
    int capacity = input.Count;
    if (capacity> 1)
        {
        for (int i = 0; i < capacity * 3; i++)
            {
            int r1 = shuffle.Next(capacity);
            int r2 = shuffle.Next(capacity);
            T temp = input[r1];
            input[r1] = input[r2];
            input[r2] = temp;
            }
        }
    }


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

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