随机long(Random long/NextLong)

  • Post author:
  • Post category:其他


在.net FCL中random没有生成long类型的随机数,不过random可以生成0-1的随机数,这就可以帮助实现random Long.

如下为一简单实现(.net 3.5):

ContractedBlock.gif
ExpandedBlockStart.gif

Random NextLong


1




static




class


Common



2


{




3




public




static




long


NextLong(


this


Random random,

long

minValue,

long

maxValue)



4


{




5




if


(minValue


>


maxValue)



6


{




7




throw




new


ArgumentException(





minValue is great than maxValue





,





minValue





);



8


}



9




long


num


=