How can i generate a random string that can contain either plus sign or minus sign (- or +)

只愿长相守 提交于 2020-01-07 08:11:11

问题


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

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

But that doeen't work, Anyone have a way I can do this? Note: Im a beginner


回答1:


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("-", "+");



回答2:


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



回答3:


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



回答4:


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 ? "-" : "+";



回答5:


Something like this:

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

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



回答6:


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



回答7:


try

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

fiddle



来源:https://stackoverflow.com/questions/43851416/how-can-i-generate-a-random-string-that-can-contain-either-plus-sign-or-minus-si

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!