简体   繁体   English

如何在C#中使用十进制值生成随机数

[英]How to generate a Random number using Decimal value in C#

I have a vb6 code which generate the random number. 我有一个生成随机数的vb6代码。 The Query is Like this: 查询是这样的:

"xyz= Format(10000 * Rnd(-Rnd(-Val(valueData / 10000))), "000000") “ xyz = Format(10000 * Rnd(-Rnd(-Val(valueData / 10000))),” 000000“)

I want to convert in to C# code. 我想转换为C#代码。 Currently i am using Dotnet 3.5. 目前,我正在使用Dotnet 3.5。 "valueData" is a return value and it is a string. “ valueData”是一个返回值,它是一个字符串。 In vb6 it is working fine. 在vb6中,它工作正常。 but in C# when i converting to integer it is not returning any decimal value. 但是在C#中,当我转换为整数时,它不返回任何十进制值。 But when i change it like bellow i am getting the decimal value but I am not able to pass it to random number function. 但是当我像波纹管一样更改它时,我得到的是十进制值,但我无法将其传递给随机数函数。 because it is accepting only integer. 因为它只接受整数。

sss= Convert.ToDecimal(valueData ) / 1000000; sss = Convert.ToDecimal(valueData)/ 1000000;

I meed to convert the above code in to C#. 我需要将上面的代码转换为C#。 any help will be usefull for me." 任何帮助对我都是有用的。”

Blurgh! Blurgh! Forget that VB6 code altogether. 完全忘记该VB6代码。 In .NET, create a Random object and call the appropriate method. 在.NET中,创建一个Random对象并调用适当的方法。 The NextDouble method will return a Double value d where 0 <= d < 1. You can then do whatever scaling or translation or rounding you need to massage that into the desired range. NextDouble方法将返回一个Doubled ,其中0 <= d <1。然后,您可以执行任何缩放,平移或舍入操作,将其调整到所需的范围。 DO NOT at any point convert the number to a String to get what you want. 请勿在任何时候将数字转换为String以获得所需的内容。 That's an example of why VB6 gets looked down on by those who use other languages. 这就是为什么使用其他语言的人看不起VB6的一个例子。

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

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