0

I'm trying to create a scrubbing program and I'm stuck on a bit where I have to generate a set of random numbers to replace a string of numbers and while I can get the random number once, I can't seem to figure out how to make it replace the entire nine character string.

public static int GetRandomNumber()
        {
            Random rnd = new Random();
            // creates a number between 0 and 9
            int ranNum = rnd.Next(10);
            return ranNum;
        }

I know it has something to do with checking against the string length and repeating until it replaces the entire string but I can't for the life of me remember how and googling is a bit too non-specific. The string itself is being pulled from a text file and has been split into an array.

public static string[] ScrubData(string line)
        {
            string[] words = line.Split('|');
            replaceData(words);
            MessageBox.Show(words[0] + words[2]);
            return words;
        }

        private static void replaceData(string[] words)
        {

            words[0] = Convert.ToString(GetRandomNumber());            
        }

I know someone already asked a similar question, but I have no idea what "lock" is or how to connect their question with mine.

2 Answers2

0

Not sure I completely understood, but it sounds like you want a method that returns a string of random numbers that is the same length as th input string? If so, this should work:

private static Random _rnd = new Random();

public static string ReplaceCharactersWithRandomNumbers(string input)
{
    if (input == null) return null;

    var newString = new StringBuilder();

    for (int i = 0; i < input.Length; i++)
    {
        newString.Append(_rnd.Next(10));
    }

    return newString.ToString();
}

Update

I modified the code to initialize the Random outside the method. This will ensure unique numbers each time you call it. If the object is initialized in the method, it may get seeded with the same value each time it's called, and will generate the same numbers each time.

If you want to be tricky, here's a one-line way to do it:

private static readonly Random Rnd = new Random();

public static string ReplaceCharactersWithRandomNumbers(string input)
{
    return input == null ? null : string.Join("", input.Select(c => Rnd.Next(10)));
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • AH, thank you both, I completely blanked on for (int i = 0; i < input.Length; i++) for some reason. Didn't know about string builder, but it rings a bell. I'll have to recheck my notes. – Action Frank May 13 '15 at 20:56
  • `StringBuilder` is a more efficient way to concatenate strings (using `.Append`), but the type of `newString` could be changed to a `string`, and then in the body of the `for` you could just do `newString += rnd.Next(10).ToString();` – Rufus L May 13 '15 at 22:11
  • One other thing to note, is that the creation of `Random` should really be outside of this method. If you call this method with the same string in rapid succession, they will all end up with the same random characters. I will update the answer to reflect a better way. – Rufus L May 13 '15 at 22:15
-1

You need to enumerate the string and rebuild the entire string. It looks like your input string is a set of tokens separated with tee ("|") marks. I use the StringBuilder because concatenation of a lot of strings is slow in .NET.

Try this ... or something similar:

    public static string ScrubData(string line)
    {
        string[] words = line.Split('|');
        System.Text.StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.Length; i++)
        {
            sb.Append(GetRandomNumber().ToString());
            if (i + 1 < words.Length)
                sb.Append("|");
        }
        return sb.ToString();
    }
Marc Johnston
  • 1,276
  • 1
  • 7
  • 16