aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/src
diff options
context:
space:
mode:
authorSverker Eriksson <[email protected]>2011-09-19 11:04:58 +0200
committerSverker Eriksson <[email protected]>2011-09-19 11:04:58 +0200
commit307c06863caebcd63f98c010a31a0263890af9b4 (patch)
treee47f800cb8b63e886b1b141567e80f24096a3b20 /lib/crypto/src
parent7ebf84abf7e5d06ce9ef6ec11783318aa393093d (diff)
parent02b8d9b4d669ae215b1c2423fa961db1b69a487f (diff)
downloadotp-307c06863caebcd63f98c010a31a0263890af9b4.tar.gz
otp-307c06863caebcd63f98c010a31a0263890af9b4.tar.bz2
otp-307c06863caebcd63f98c010a31a0263890af9b4.zip
Merge branch 'sverker/crypto-rand_uniform-negative/OTP-9526' into dev
* sverker/crypto-rand_uniform-negative/OTP-9526: [crypto] Fix rand_uniform for negative values
Diffstat (limited to 'lib/crypto/src')
-rw-r--r--lib/crypto/src/crypto.erl11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index c35dfcebab..c3e13d6b91 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -415,6 +415,13 @@ rand_uniform(From,To) when is_binary(From), is_binary(To) ->
Whatever
end;
rand_uniform(From,To) when is_integer(From),is_integer(To) ->
+ if From < 0 ->
+ rand_uniform_pos(0, To - From) + From;
+ true ->
+ rand_uniform_pos(From, To)
+ end.
+
+rand_uniform_pos(From,To) when From < To ->
BinFrom = mpint(From),
BinTo = mpint(To),
case rand_uniform(BinFrom, BinTo) of
@@ -422,7 +429,9 @@ rand_uniform(From,To) when is_integer(From),is_integer(To) ->
erlint(Result);
Other ->
Other
- end.
+ end;
+rand_uniform_pos(_,_) ->
+ error(badarg).
rand_uniform_nif(_From,_To) -> ?nif_stub.