简体   繁体   English

Unity & C# - 从数组中选择一个随机字符串

[英]Unity & C# - Choosing a random string from an array

I'm trying to make a simple rock paper scissors game.我正在尝试制作一个简单的石头剪刀布游戏。 I need the computer to randomly choose a string from the array of ROCK, PAPER, and SCISSORS.我需要计算机从 ROCK、PAPER 和 SCISSORS 数组中随机选择一个字符串。 This is what I have so far:这是我到目前为止所拥有的:

public string GetComputerChoice()
    {
        string computerChoice = null;
        string[] computerChoices = { "ROCK", "PAPER", "SCISSORS" };

        return computerChoice[Random.Next(computerChoices.Length)];
    }

The only error I am getting in Visual Studio is for 'Next' which says 'Random does not contain a definition for 'Next'.我在 Visual Studio 中遇到的唯一错误是“下一个”,它表示“随机不包含“下一个”的定义。

I am entirely new to programming as a whole.我对整个编程完全陌生。 Any tips for why this isn't working or what I could do to make it work?关于为什么这不起作用或我可以做些什么来使它起作用的任何提示? I've read other replies on similar posts but it seems all the answers are just blocks of code to make it work with no explanation for why it works.我已经阅读了关于类似帖子的其他回复,但似乎所有答案都只是代码块,以使其工作,而没有解释它为什么工作。

The Next() method isn't static, so you need to instantiate a Random object to use it: Next()方法不是 static,因此您需要实例化一个Random object 才能使用它:

public string GetComputerChoice()
{
    string computerChoice = null;
    string[] computerChoices = { "ROCK", "PAPER", "SCISSORS" };

    return computerChoice[new Random().Next(computerChoices.Length)];
}

However, creating a new Random object every time you want a random value is not best practice and can lead to unexpected results such as duplicate values, especially when invoked in quick succession since the current time is used in the PRNG seed value (read how pseudo random number generators work).但是,每次需要随机值时都创建一个新的Random object 并不是最佳实践,并且可能导致意外结果,例如重复值,尤其是在快速连续调用时,因为 PRNG 种子值中使用了当前时间(请阅读伪随机数生成器工作)。 Ideally, you'd create a Random object one time and store it somewhere for repeated use.理想情况下,您会创建一个Random object 并将其存储在某个地方以供重复使用。

I'm not very familiar with Unity at all, but it looks like it has its own Random class to help you out: Unity Random - Documentation我对 Unity 一点也不熟悉,但它看起来有自己的随机 class 来帮助你: Unity Random - Documentation

you should replace line below你应该替换下面的行

return computerChoice[Random.Next(computerChoices.Length)];

by this line通过这条线

return computerChoice[new Random().Next(computerChoices.Length)];

why?:为什么?:
Because Random is a static class and you cannot make an instance of it by the new keyword.因为 Random 是static class 并且您不能通过new关键字创建它的实例。 The reason is that static classes are shared between threads (contexts that contains your requests) and user a that made request use exactly the same class in the same memory location that user b use it.原因是 static 类在线程(包含您的请求的上下文)和发出请求的user a之间共享,在user b使用它的同一memory location使用exactlysame class

You can think of static class as a shared room that multiple families are using it and it does not belong just to you,then you are not authorized to make any change,you can just use it!你可以把static class想象成一个shared room ,多个家庭都在使用它,它不属于你,那么你无权做任何改变,你可以使用它!

when you use new keyword in your code,actually you are making an instance of that class that just belong to you ( you:means request made by you).当您在代码中使用new关键字时,实际上您正在创建一个that just belong to you making an instance of that class (您:表示您提出的请求)。

romove computerChoice and replace line below romove computer选择并替换下面的行

return computerChoice[Random.Next(computerChoices.Length)];

by this line通过这条线

return computerChoices [new System.Random().Next(computerChoices.Length)];

like this:像这样:

   public string GetComputerChoice()
{
   
    string[] computerChoices = { "ROCK", "PAPER", "SCISSORS" };

    return computerChoices [new System.Random().Next(computerChoices.Length)];
}

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

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