简体   繁体   English

C#随机数生成器

[英]C# Random Number Generator

I'm using the following code to generate 4 random numbers, but each time I try to get the they're coming up as "system.random" in the textblock. 我正在使用以下代码生成4个随机数,但是每次尝试获取它们时,它们都会在文本块中显示为“ system.random”。 Help anyone? 帮助任何人? Thanks!! 谢谢!! :) :)

private void Button_Click_1(object sender, RoutedEventArgs e)        
{           
    Random dc1 = new Random();
    int dealCard1 = dc1.Next(52);

    Random dc2 = new Random();
    int dealCard2 = dc2.Next(52);

    Random pc1 = new Random();
    int playerCard1 = pc1.Next(52);

    Random pc2 = new Random();
    int playerCard2 = pc2.Next(52);

    txtDC1.Text = Convert.ToString(dc1);
    txtDC2.Text = Convert.ToString(dc2);
    txtPC1.Text = Convert.ToString(pc1);
    txtPC2.Text = Convert.ToString(pc2);
}

First off, you only need one random instance: 首先,您只需要一个随机实例:

Random dc1 = new Random();
int dealCard1 = dc1.Next(52);
int dealCard2 = dc1.Next(52);
int playerCard1 = dc1.Next(52);
int playerCard2 = dc1.Next(52);

The errors are because you're not reporting the numbers: 错误是因为您没有报告数字:

txtDC1.Text = Convert.ToString(dealCard1);
txtDC2.Text = Convert.ToString(dealCard2);
txtPC1.Text = Convert.ToString(playerCard1);
txtPC2.Text = Convert.ToString(playerCard2);

You are converting the random number generator, not the random number. 您正在转换随机数生成器,而不是随机数。

Your random number generators are dc2 , pc1 , and pc2 . 您的随机数生成器是dc2pc1pc2 Your random numbers are dealCard2 , playerCard1 , and playerCard2 . 您的随机数是dealCard2playerCard1playerCard2

From here you should be able to solve the problem. 从这里您应该能够解决问题。

Taking just one for simplicity: 为了简单起见,只取一个:

Random dc1 = new Random();
int dealCard1 = dc1.Next(52);
txtDC1.Text = Convert.ToString(dc1);

Here you've converted dc1 , which is a Random object to a string, not dealCard1 , which is the random number. 在这里,您已经将dc1 (这是一个Random对象)转换为字符串,而不是dealCard1 (这是随机数)。

txtDC1.Text = Convert.ToString(dealCard1);

And so on. 等等。

(Note also, if the idea here is to pick for out of 52 cards, as with a playing card deck, that you aren't checking you don't have more than one of the exact same card, which may or may not be a problem). (另外请注意,如果这里的想法是从52张纸牌中挑选一张,例如使用扑克牌组,则表示您不是在检查自己是否拥有一张或多张完全相同的纸牌,这可能是也可能不是一个问题)。

You are putting the Random object itself into the textblock, hence "System.Random". 您将Random对象本身放入文本块,因此为“ System.Random”。

Your code should be: 您的代码应为:

txtDC1.Text = Convert.ToString(dealCard1);
txtDC2.Text = Convert.ToString(dealCard2);
txtPC1.Text = Convert.ToString(playerCard1);
txtPC2.Text = Convert.ToString(playerCard2);

You also shouldn't recreate the Random instance each time. 您也不应该每次都重新创建Random实例。 Just use one (ideally as a class level variable). 只需使用一个(理想情况下用作类级别的变量)。 This prevents re-seeding the Random object with (nearly) the same seed value (which is based on the system time). 这样可以防止(几乎)使用相同的种子值(基于系统时间)重新播种随机对象。 As is, each of your "random" numbers could very well be identical! 照原样,您的每个“随机”数字都可能完全相同!

First of all you're converting the wrong variable, what you want is 首先,您要转换错误的变量,您想要的是

txtDC1.Text = Convert.ToString(dealCard1);
txtDC2.Text = Convert.ToString(dealCard2);
txtPC1.Text = Convert.ToString(playerCard1);
txtPC2.Text = Convert.ToString(playerCard2);

Also try to avoid creating a new instance of random every time you call the click event. 还要避免每次调用click事件时都创建一个随机的新实例。 Consider creating one random instance that can be reused. 考虑创建一个可以重用的随机实例。

If you must create a new instance at every event, then consider adding a seed to the random, as quick succession of events may cause the Random instance to be seeded with the same seed and thus causing repeated values. 如果必须在每个事件上都创建一个新实例,则可以考虑向随机数添加一个种子,因为事件的快速连续可能会导致Random实例使用相同的种子进行播种,从而导致重复的值。

I had trouble with this before and the error that I had was that I had too many random instance. 我以前对此很麻烦,而我的错误是我有太多随机实例。 So by looking at your code I see you got more than one. 因此,通过查看您的代码,我看到了不止一个。 I would alter the coding to have only one random instance. 我将更改编码以仅具有一个随机实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM