虽然我使用了 Rnd 函数,但它并没有真正给出随机数,我如何修改我现有的代码以获得随机的、不同的值?
Although I've used Rnd Function, it doesn't TRULY give random numbers, how can I modify my existing code to gain randomly, distinct values?
Sub Monte_Carlo_Integration_Of_A_Function()
Dim x As Double, y As Double, num As Integer
Randomize()
Console.WriteLine()
Console.WriteLine("Monte Carlo Integration")
Console.WriteLine("XXXXXXXXXXXXXXXXXXXXXXX")
Console.WriteLine("Please Enter A Number?")
num = Console.ReadLine()
x = Rnd()
y = Rnd()
For i = 1 To num
Console.WriteLine(x)
Console.WriteLine(y)
Console.WriteLine()
Next
如何修改此代码以获得不同的随机数
而不是在进入 For
循环之前获取下一组随机数,像这样:
x = Rnd() ' Assign next random number to x
y = Rnd() ' Assign next random number to y
For i = 1 To num
Console.WriteLine(x) ' Display the value of x
Console.WriteLine(y) ' Display the value of y
Console.WriteLine()
Next
您需要在 For
循环中获取下一组随机数,以便为循环的每次迭代生成一组新的随机数:
For i = 1 To num
x = Rnd() ' Assign next random number to x
y = Rnd() ' Assign next random number to y
Console.WriteLine(x) ' Display the value of x
Console.WriteLine(y) ' Display the value of y
Console.WriteLine()
Next
但是,Randomize
和 Rnd
是旧的 VB6 样式命令,它们大多只在 VB.NET 中仍然可用以实现向后兼容。在新的VB.NET开发中,建议大家使用Random
class,像这样:
Dim r As New Random()
For i = 1 To num
x = r.NextDouble()
y = r.NextDouble()
Console.WriteLine(x)
Console.WriteLine(y)
Console.WriteLine()
Next
Sub Monte_Carlo_Integration_Of_A_Function()
Dim x As Double, y As Double, num As Integer
Randomize()
Console.WriteLine()
Console.WriteLine("Monte Carlo Integration")
Console.WriteLine("XXXXXXXXXXXXXXXXXXXXXXX")
Console.WriteLine("Please Enter A Number?")
num = Console.ReadLine()
x = Rnd()
y = Rnd()
For i = 1 To num
Console.WriteLine(x)
Console.WriteLine(y)
Console.WriteLine()
Next
如何修改此代码以获得不同的随机数
而不是在进入 For
循环之前获取下一组随机数,像这样:
x = Rnd() ' Assign next random number to x
y = Rnd() ' Assign next random number to y
For i = 1 To num
Console.WriteLine(x) ' Display the value of x
Console.WriteLine(y) ' Display the value of y
Console.WriteLine()
Next
您需要在 For
循环中获取下一组随机数,以便为循环的每次迭代生成一组新的随机数:
For i = 1 To num
x = Rnd() ' Assign next random number to x
y = Rnd() ' Assign next random number to y
Console.WriteLine(x) ' Display the value of x
Console.WriteLine(y) ' Display the value of y
Console.WriteLine()
Next
但是,Randomize
和 Rnd
是旧的 VB6 样式命令,它们大多只在 VB.NET 中仍然可用以实现向后兼容。在新的VB.NET开发中,建议大家使用Random
class,像这样:
Dim r As New Random()
For i = 1 To num
x = r.NextDouble()
y = r.NextDouble()
Console.WriteLine(x)
Console.WriteLine(y)
Console.WriteLine()
Next