简体   繁体   中英

How can I make a program generate a random number with a set pattern to follow in c# with .net framework?

I am familiar with generating random numbers, but this would be more like a code I guess. What I want it to do is follow a specified pattern and generate numbers for every x in the code while there are some set numbers. The code pattern would be something like this: xx0xxxx00xx0. I want the program to make the x's into something random, but keep the zero's. If you have any questions or any way I could make my question better, feel free to comment, Also if this is a duplicate question. just tell me and I can remove it. Thanks!

You could try using regex. A simple example to get you started would be:

    static void Main()
    {
        var code = "xx0xxxx00xx0";

        var numStr = Regex.Replace(code, @"([^0-9.])", x => new Random().Next(1,9).ToString());

        Console.WriteLine(numStr);
    }

    // 450871700340

With this approach, you could really give it any sort of code with that x0 format you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM