1

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. 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'.

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.

  • I suspect a namespace conflict `System.Random` vs `UnityEngine.Random` ... You could use `Random.Range(0, computerChoices.Length)` – derHugo Jul 11 '20 at 18:41
  • Also does this answer your question: https://stackoverflow.com/questions/2019417/how-to-access-random-item-in-list ? – derHugo Jul 11 '20 at 18:45

3 Answers3

0

The Next() method isn't static, so you need to instantiate a Random object to use it:

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). Ideally, you'd create a Random object one time and store it somewhere for repeated use.

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

itsme86
  • 19,266
  • 4
  • 41
  • 57
0

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. 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 .

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!

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).

Abolfazl
  • 1,592
  • 11
  • 32
0

romove computerChoice and replace line below

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)];
}