简体   繁体   English

解释此c#方法的代码如何工作以及每个部分在做什么

[英]explain how does the code of this c# method workes.and what each part is doing

this is a method in c# that is used in the program to makes a 10-digit number. 这是c#中的一种方法,在程序中使用该方法来生成10位数字。 my question is that how does this work exactly? 我的问题是,这到底如何工作? what is the code doing? 代码在做什么?

long LongRandom(long min,long max,Random rand)
        {
            long result = rand.Next((Int32)(min >> 32), (Int32)(max >> 32));
            result = (result << 32);
            result = result | (long)rand.Next((Int32)min, (Int32)max);
            return result;
        }

Just consider the following analogy with decimals (for simplicity, as we're not computers): 只需考虑以下与小数的类比(为简单起见,因为我们不是计算机):

Let's say we have a random generator that can go from 0 to 999, and you want to generate a random number between two numbers in a range [0, 999.999] . 假设我们有一个可以从0到999的随机数生成器,并且您想在[0, 999.999]范围内的两个数字之间生成一个随机数。

What this code does is take anything above 999 in your min and max , and generate a random number for thousands, then take anything below 1.000 in your min and max and generate a random number for units below a thousand, then add those values. 该代码的作用是将minmax大于999的值取max ,并生成一千的随机数,然后取minmax中的1.0000以下的值取数,并为一千以下的单元生成一个随机数,然后将这些值相加。

However, if you wanted to get a random number between say 20 and 1.080 using this method, you will never be able to get anything in the range [81, 1019] for example. 但是,如果您想使用此方法获得介于201.080之间的随机数,例如,您将永远无法获得[81, 1019]范围内的任何值。

Also note that there's a problem with signed/unsigned values in your code (as stated by jdweng) 另请注意,代码中的有符号/无符号值存在问题(如jdweng所述)

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

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