Refactor next_i32 method to handle edge case
This commit is contained in:
parent
05b99a609f
commit
fe46122537
@ -165,21 +165,26 @@ impl RC4 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 生成 i32 随机数
|
/// 生成 i32 随机数
|
||||||
/// ```dart
|
/// ```javascript
|
||||||
/// int nextInt(int max) {
|
/// ax(a) {
|
||||||
/// int round = max ;
|
/// // nextInt
|
||||||
/// int v = nextByte();
|
/// var n, round
|
||||||
/// do {
|
/// if (a === 0) return 0
|
||||||
/// v = v <<8 | nextByte();
|
/// n = this.n()
|
||||||
/// if (v >= max) {
|
/// round = a
|
||||||
/// v %= max;
|
/// do {
|
||||||
/// }
|
/// n = (n << 8 | this.n()) >>> 0
|
||||||
/// round >>= 6;
|
/// if (n >= a) n = C.JsInt.V(n, a)
|
||||||
/// } while(round != 0);
|
/// round = C.JsInt.am(round, 8)
|
||||||
/// return v;
|
/// } while (round !== 0)
|
||||||
|
/// return n
|
||||||
/// }
|
/// }
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub fn next_i32(&mut self, max: i32) -> i32 {
|
pub fn next_i32(&mut self, max: i32) -> i32 {
|
||||||
|
if max == 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
let mut round = max;
|
let mut round = max;
|
||||||
let mut v = self.next_u8() as i32;
|
let mut v = self.next_u8() as i32;
|
||||||
loop {
|
loop {
|
||||||
@ -187,7 +192,8 @@ impl RC4 {
|
|||||||
if v >= max {
|
if v >= max {
|
||||||
v %= max;
|
v %= max;
|
||||||
}
|
}
|
||||||
round >>= 6;
|
round >>= 8;
|
||||||
|
// rc4 lib 里是 6, md5.js( 继承的 R ) 里是 8
|
||||||
if round == 0 {
|
if round == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user