Adjust RNG algorithm
This commit is contained in:
parent
86695aa67e
commit
5c185b386c
@ -6,6 +6,8 @@ The algorithm takes a *seed* value, which is an arbitrary character string. The
|
|||||||
|
|
||||||
In order to generate a value between 0 (inclusive) and *n* (exclusive), to the state is appended a comma (",") followed by the value of the counter, and a SHA-256 hash *H* is computed of the resulting string encoded using UTF-8. The hash *H* is represented as an unsigned hexadecimal integer, *k*. The counter is incremented by 1.
|
In order to generate a value between 0 (inclusive) and *n* (exclusive), to the state is appended a comma (",") followed by the value of the counter, and a SHA-256 hash *H* is computed of the resulting string encoded using UTF-8. The hash *H* is represented as an unsigned hexadecimal integer, *k*. The counter is incremented by 1.
|
||||||
|
|
||||||
In order to avoid modulo bias, if *k* ≥ ⌊*M*/*n*⌋ × *n* (where *M* is the maximum possible value of *k*, 2^256 - 1), *k* is discarded and the algorithm is repeated.
|
In order to avoid modulo bias, if *k* ≥ ⌊*M*/*n*⌋ × *n* (where *M* = 2^256), *k* is discarded and the algorithm is repeated.
|
||||||
|
|
||||||
Otherwise, the result is *k* modulo *n*.
|
Otherwise, the result is *k* modulo *n*.
|
||||||
|
|
||||||
|
In revision [86695aa](https://yingtongli.me/git/pyRCV2/commit/?id=86695aa67e2af936a84d78cb7a7aed9844f61ca9) and older, a slightly different formula was in use to avoid modulo bias – see the documentation of previous revisions for details. It is theoretically possible, though overwhelmingly unlikely, that this may result in different outcomes.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# pyRCV2: Preferential vote counting
|
# pyRCV2: Preferential vote counting
|
||||||
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
|
# Copyright © 2020–2021 Lee Yingtong Li (RunasSudo)
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -15,7 +15,7 @@
|
|||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
|
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
|
||||||
_MAX_VAL = bigInt(2).pow(256).subtract(1)
|
_MAX_VAL = bigInt(2).pow(256)
|
||||||
else:
|
else:
|
||||||
# Fail gracefully if dependencies not present
|
# Fail gracefully if dependencies not present
|
||||||
_MAX_VAL = None
|
_MAX_VAL = None
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# pyRCV2: Preferential vote counting
|
# pyRCV2: Preferential vote counting
|
||||||
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
|
# Copyright © 2020–2021 Lee Yingtong Li (RunasSudo)
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -17,7 +17,7 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
class SHARandom:
|
class SHARandom:
|
||||||
MAX_VAL = 2 ** 256 - 1
|
MAX_VAL = 2 ** 256
|
||||||
|
|
||||||
def __init__(self, seed):
|
def __init__(self, seed):
|
||||||
self.seed = seed
|
self.seed = seed
|
||||||
|
Reference in New Issue
Block a user