C#中编写随机类所需的帮助 [英] help needed in C#to write a random class

查看:72
本文介绍了C#中编写随机类所需的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#设计一个Windows窗体应用程序,该应用程序将名称无重复地随机分布到不同的表中,所以我想知道如何编写随机分布类
编程新手,所以我不知道该怎么做
谢谢

Pls i want to design a windows form application using C# that distributes names into different tables without repetition and randomly so i want to knw how to write a random distribution class
Am new to programming so i dont know how to go about this
Thanks

推荐答案

使用Random类,您可以整天生成随机数.
The Random class lets you generate random numbers all day long.


创建一系列您想要的东西随机分配并一次取出一个东西.

在代码中-创建一个List<yourthing>并用您希望分发的所有内容填充它.使用Random类选择一个介于0和列表中的事物数之间的数字(-1).获取值并删除该项目,然后重复执行直到列表为空.
Create a bucket of the things you want to distribute randomly and pull things out one at a time.

In code - create a List<yourthing> and populate it with everything you wish to distribute. Use the Random class to pick a number between 0 and the number of things in the list (-1). Get the value and remove the item then repeat until the list is empty.


如果您需要提取不重复的随机数(可能是名称数组的索引),那么请看一下在我的提示中从卡片组中随机抽取5张卡片" [ ^ ](我知道这是C++代码,但无论如何,只是算法很重要).
If you need to extract random numbers (might be the indices of your array of names) without repetitions then have a look at my tip "Random extraction of 5 cards from a deck"[^] (it is C++ code, I know, but, anyway, just the algo matters).


这篇关于C#中编写随机类所需的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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