如何获取应该是唯一的随机数列表 [英] How to get a list of random numbers that should be unique

查看:47
本文介绍了如何获取应该是唯一的随机数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我想要包含 5 个随机数的列表,该列表应该包含从 1 到 5 的唯一值?我怎样才能做到这一点?

In C#, I want to have the list of 5 random numbers, that should contain unique values from 1 to 5? How can I achieve that?

例如:1,3,2,5,4 或 2,5,1,3,4

for example: 1,3,2,5,4 or 2,5,1,3,4

即该列表必须包含 5 个随机数,这些数应该是从 1 到 5 的数字

i.e. The list must contains 5 random numbers that should be having the numbers from 1 to 5

推荐答案

Random rnd = new Random();
var list = Enumerable.Range(1, 5).OrderBy(x => rnd.Next()).ToList();

这篇关于如何获取应该是唯一的随机数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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