如何在 vb.net 中创建随机数生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28666170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:49:48  来源:igfitidea点击:

How do I create a random number generator in vb.net

vb.netrandom

提问by Eagler

Hi i want to make a random number generator that just creates random expressions

嗨,我想制作一个只创建随机表达式的随机数生成器

This is for a math game project i have. My problem is that right now i dont understand the random generator.

这是我的一个数学游戏项目。我的问题是现在我不了解随机生成器。

Like i just want it to create a random 2 digit integer then an operation then another random 2 digit integer. One random expression generator will generate subtraction and addition problems, while another will generate multiplication and division problem

就像我只是想让它创建一个随机的 2 位整数,然后是一个操作,然后是另一个随机的 2 位整数。一个随机表达式生成器会产生减法和加法问题,而另一个会产生乘法和除法问题

like this

像这样

23 - 78 48-55 24 + 16

23 - 78 48-55 24 + 16

回答by paxdiablo

You're probably going to find it easier to figure out how the current generator works than writing your own.

您可能会发现弄清楚当前生成器的工作方式比编写自己的生成器更容易。

There is an expression that will give you a random integer between two inclusive bounds:

有一个表达式可以为您提供两个包含边界之间的随机整数:

value = CInt(Math.Floor((upper - lower + 1) * Rnd())) + lower

and you can use that to get your two-digit numbers:

你可以用它来得到你的两位数:

value = CInt(Math.Floor(90 * Rnd())) + 10

To get a basic operation from the set of four {+-*/}, you can use the same rules to give you a random value from zero to three inclusive:

要从一组四中获得基本操作{+-*/},您可以使用相同的规则为您提供一个从零到三(含)的随机值:

op = CInt(Math.Floor(4 * Rnd()))

If you want a textualrepresentation of that, you simply need a string that you can get the operator from (as character or string), something like:

如果您想要它的文本表示,您只需要一个可以从中获取运算符的字符串(作为字符或字符串),例如:

Dim ops As String = "+-*/"
Dim chrop as Char = ops(CInt(Math.Floor(4 * Rnd())))
Dim strop as String = ops.SubString(CInt(Math.Floor(4 * Rnd())),1)


Here, for example, is a complete program to generate such expressions, though you'll need to figure out how to integrate that with your own code (refactoring it into a couple of functions would be nice for a start).

例如,这里有一个完整的程序来生成这样的表达式,尽管您需要弄清楚如何将它与您自己的代码集成(将它重构为几个函数会很好的开始)。

And I haven't done an exhaustiveanalysis on whether there are edge cases in the number generation so you'll need a test suite to be certain.

而且我还没有对数字生成中是否存在边缘情况进行详尽的分析,因此您需要一个测试套件来确定。

All things you should be doing as a developer anyway :-)

无论如何,您作为开发人员应该做的所有事情:-)

Module Module1
    Sub Main()
        Dim ops As String = "+-*/"
        Randomize()
        Dim value1 As Integer = CInt(Math.Floor(90 * Rnd())) + 10
        Dim value2 As Integer = CInt(Math.Floor(90 * Rnd())) + 10
        Dim strop As String = ops.Substring(CInt(Math.Floor(4 * Rnd())), 1)
        Console.WriteLine(value1 & " " & strop & " " & value2)
    End Sub
End Module

回答by Chris Dunaway

Perhaps the Randomclass would be useful:

也许这Random门课会很有用:

Sub Main

    'Create an instance of the Random class
    Dim rnd As New Random()

    'Get a random number from 10 to 99  (2 digits)
    Dim randomNumber As Integer = rnd.Next(10, 100)

    Console.WriteLine(randomNumber)

End Sub