Adding an 'if' statement

Refresh

March 2019

Views

94 time

1

I'm trying to get a number generator to work and its going good so far, I have encountered a few problems as of lately though. So what im trying to do is make a generator with a default string then add some numbers to the end of it. I already have all of that figured out. Now, I am trying to make it so that if the number my generator spits out is '5' then the next number would have to be a '1'. Im not entirely sure if this is possible but here is my Code:

    public string GenerateRandomCode(int length)
    {
        string charPool = "ABCDEF1234567890";
        StringBuilder rs = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++)
        {
              rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
        }
        return rs.ToString();
    }

    public string Random(int length)
    {
        string charPool = "A457";
        StringBuilder rs = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++)
        {
            rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
        }
        return rs.ToString();
    }

    public string Char(int length)
    {
        string charPool = "ABF35689";
        StringBuilder rs = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++)
        {
             rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
        }
        return rs.ToString();
     }

     public string Next(int length)
     {
         string charPool = "F01";
         StringBuilder rs = new StringBuilder();
         Random random = new Random();

         for (int i = 0; i < length; i++)
         {
             rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
         }
         return rs.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       textBox1.Text = "00000001008" + Random(1) + "000" + Char(1) + Next(1) + GenerateRandomCode(15);
    }
  }
}

So if Char = '5' I would like to get Next to equal '1'

2 answers

0

Not sure what you are shooting at, but is it perhaps something like this(?):

private void button1_Click(object sender, EventArgs e)
{
   string sRandom = Random(1);
   string sChar = Char(1);
   string sNext = "1";
   if (sChar != "5") sNext = Next(1);

   textBox1.Text = "00000001008" + sRandom + "000" + sChar + sNext + GenerateRandomCode(15);
}

So, whenever the method call of Char(1) returns the string "5", sNext will keep its initial value of "1". Otherwise, if Char(1) is not "5", sNext will be assigned the result of the call of Next(1).

2

Shure you can. Remember the last character and act in consequence.

// ...
char c = '-';
for (int i = 0; i < length; i++) {
    if (c == '5') {
        c = '1';
    } else {
        c = charPool[random.Next(charPool.Length)];
    }
    rs.Append(c);
}
// ...

random.Next(charPool.Length) automatically yields an integer in the range [0 ... charPool.Length-1].