user7975284
user7975284

Reputation:

How can I pick randomly between a minus and plus sign?

I'm making a math game, and I need to make it randomly pick between- or +.
I have already tried this:

Random rnd = new Random();
string name = rnd.Next(-, +);  

But that doesn't work. Anyone have a way I can do this?

Upvotes: 0

Views: 779

Answers (7)

public static class Extensions
{
    public static T NextItem<T>(this Random r, params T[] items)
    {
        //  Thanks to nbokmans for catching my error here: The second parameter
        //  is not the largest value it can return; it's the "exclusive upper bound".
        return items[r.Next(0, items.Length)];
    }
}

...

Random rnd = new Random();
string name = rnd.NextItem("-", "+");

Upvotes: 3

CodeBox
CodeBox

Reputation: 446

try

    String[] symbols = new[]{"-", "+"};
    Random r = new Random();
    var sym = symbols[r.Next(0, 2)];

fiddle

Upvotes: 0

nbokmans
nbokmans

Reputation: 5747

As a commenter mentioned the Random class does not let you generate a random letter. However, it does offer a random number generation function: Next(). See https://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx

So, to generate a random + or - becomes really trivial with that - we just create an array containing the possible options, and generate a random number between 0 and the length of the array. You might think, but wait, if my array contains 2 items, and the random number is 2, and I try to access the array at index 2, wouldn't I get an index out of bounds exception? The answer to that is no, because the upper bound is exclusive in the Next() function - it can return anything between 0 and upperBound - 1.

char[] options = {'+', '-'};
Random rnd = new Random();
int randomNumber = rnd.Next(0, options.Length);
char randomChar = options[randomNumber];

Upvotes: 6

FutureTechLab
FutureTechLab

Reputation: 162

I'm guessing you want to flip a coin to get either a positive or a negative number? If you just want a string that's either + or - try this:

string name = (rnd.NextDouble() > 0.5)? "+" : "-";

Then you can concatenate if that is what you are trying to do:

name = name + rnd.Next(); // Or whatever

Upvotes: 0

Alex Sikilinda
Alex Sikilinda

Reputation: 3013

Solution using LINQ:

static void Main(string[] args)
{
    Random random = new Random();
    int count = 10;
    char[] plusesAndMinuses = new int[count]
        .Select(i => random.Next())
        .Select(i => i % 2 == 0 ? '+' : '-')
        .ToArray();

    string result = new string(plusesAndMinuses);
    Console.WriteLine(result);
}

Upvotes: 1

rory.ap
rory.ap

Reputation: 35290

Something like this:

Random rnd = new Random();
string value = "";

switch (rnd.Next(0, 2))
{
    case 0:
        value = "+";
        break;
    case 1:
        value = "-";
        break;
}

Upvotes: 0

mrogal.ski
mrogal.ski

Reputation: 5930

Random.Next() will return random integer value. Based on that you can distinguish if it should be + or -. Example :

Random random = new Random(); // new Random object
int randomValue = random.Next(); // get new random value
int oneOrZero = randomValue % 2; // get either 1 or zero
string name = "";
if ( oneOrZero == 0 ) // if value is zero
{
    name = "-"; // name will contain minus sign
}
else 
{
    name = "+"; // else name will contain plus sign
}

You can simplify this to just one line :

string name = new Random().Next() % 2 == 0 ? "-" : "+";

Upvotes: 0

Related Questions