且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#中的某些给定数字中得到一个随机数?

更新时间:2022-12-10 17:42:58

有办法做到这一点,但......需要一点点工作。

随机数的全部概念就是它们:在指定的相空间上随机。所以有可能在连续的投掷中可以重复相同的数字,只需要连续六次投掷硬币,或者骰子重复投掷2。



如果您只想从特定的集合中发出号码,并且没有重复,那么您必须保留所有可能号码的列表,并将其复制到一个单独的列表中,其中一个数字是随机选择。然后从列表中删除所选的数字,以便不能再次绘制。当列表为空时,您将原始数字复制并重新开始。



这并不复杂 - 列表可以轻松处理,甚至是小样本数组设置,如果您手动洗牌你删除的那个 - 但它确实需要一些工作。





我只是想要一个简单的随机数....重复的没有。除非它变得不同......请告诉我一个简单的例子,一个简单的方法,通过代码得到随机的不......给我一个代码的小例子我不知道数组的使用很多plz告诉我siple和简单的方法一些重复是完全正常的.....



试试这个:

  private   int  [] AllowedValues =  new   int  [] { 1  2  3 , 5 , 7  11 }; 
private Random rand = new Random();

private int GetRandomNumber()
{
return AllowedValues [rand.Next(AllowedValues.Length)];
}

private void myButton_Click( object sender,EventArgs e)
{
for int i = 0 ; i < 20 ; i ++)
{
Console.WriteLine(GetRandomNumber());
}
}


将数字放在数组中,然后通过随机位置编号从数组中选择它们。你应该能够在这里得到这些想法:如何 - 从阵列中获取随机值 [ ^ ]


试试这个:

var chars = ABCDEFGHIJKLMNOPQRSTUVWX0123456789 ; // 或TextBox1.Text
var Randomchar = new Random();

var reschar = new string (Enumerable.Repeat(chars, 10 )。选择(n = > n [Randomchar.Next(n.Length)])ToArray的());


I am working on ad an app...In this app I need to get a random number among some provided numbers..I want the computer the give a number itself from the options given and with each time different number....For example i give the computer some numbers in code like 1,2,3,4,5,6 and it randomly chooses different numbers in every execution. i.e sometimes it give 3 sometimes 4 or any other...I hope i make myself clear.....Thanks in advance.....


Regards,
Ahsan Naveed

There are ways to do this, but...it takes a little work.
The whole idea of random numbers is that they are just that: random over a specified phase space. So the chances are that the same number can be repeated in successive "throws", just line a coin can come up heads six times in a row, or a dice throw a "2" repeatedly.

If you want to issue numbers only from a particular set, and with no repeats, then you have to keep a list of all "possible" numbers, and copy it into a separate list from which a number is selected at random. The selected number is then removed from the list so it can't be drawn again. When the list is empty, you copy the original numbers back and start again.

It's not complex - a list can handle it easily, or even an array for small sample sets if you manually "shuffle" the ones above the one you remove - but it does take a little work.


"i just want simple random number....The repetition of no. does not matter unless its comes different....Please just tell my a simple example an easy method by code to get random no......just give me a little example of code i dont know the use of arrays much plz tell me the siple and easy way some repetition is totally fine....."

Try this:
private int[] AllowedValues = new int[] { 1, 2, 3, 5, 7, 11 };
private Random rand = new Random();

private int GetRandomNumber()
    {
    return AllowedValues[rand.Next(AllowedValues.Length)];
    }

private void myButton_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 20; i++)
        {
        Console.WriteLine(GetRandomNumber());
        }
    }


Place the numbers in array, then pick them from the array through random position number. You should be able to get the ideas here: how-to-get-random-values-from-array-in-c-sharp[^]


Try this:
var chars = "ABCDEFGHIJKLMNOPQRSTUVWX0123456789";//or TextBox1.Text
var Randomchar = new Random();

var reschar = new string(Enumerable.Repeat(chars, 10).Select(n => n[Randomchar.Next(n.Length)]).ToArray());