简体   繁体   English

如何使用 .net 框架在 c# 中使程序生成具有设定模式的随机数?

[英]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.我想要它做的是遵循指定的模式并为代码中的每个 x 生成数字,同时有一些设定的数字。 The code pattern would be something like this: xx0xxxx00xx0.代码模式将是这样的:xx0xxxx00xx0。 I want the program to make the x's into something random, but keep the zero's.我希望程序将 x 变成随机的,但保留零。 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.使用这种方法,您可以真正为它提供您正在使用的 x0 格式的任何类型的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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