我怎样才能在一个阵列随机号码 [英] How can I randomize numbers in an array

查看:158
本文介绍了我怎样才能在一个阵列随机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的一个数组:

I have an array like this one:

int[] numbers = new [] { 1, 2, 3, 4 };

我想这(每次都不同),随机化,以便使其具有相同的大小和数量,但以不同的顺序,每次另一个数组。

I'd like to randomize this (different each time) so that it makes another array with the same size and numbers but in a different order each time.

推荐答案

尝试是这样的:

System.Random rnd = new System.Random();
var numbers = Enumerable.Range(1, 4).OrderBy(r => rnd.Next()).ToArray();

这篇关于我怎样才能在一个阵列随机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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