aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/src/crypto.erl
diff options
context:
space:
mode:
authorSverker Eriksson <[email protected]>2011-09-07 11:09:43 +0200
committerSverker Eriksson <[email protected]>2011-09-07 11:09:43 +0200
commit02b8d9b4d669ae215b1c2423fa961db1b69a487f (patch)
treef7a1b27dc3a243da40dc2e98d888e6eb245e9342 /lib/crypto/src/crypto.erl
parent22dae863060e0d66fa268f6d1cd4d6f084b850ab (diff)
downloadotp-02b8d9b4d669ae215b1c2423fa961db1b69a487f.tar.gz
otp-02b8d9b4d669ae215b1c2423fa961db1b69a487f.tar.bz2
otp-02b8d9b4d669ae215b1c2423fa961db1b69a487f.zip
[crypto] Fix rand_uniform for negative values
Also let it throw badarg if 'Hi' is not larger than 'Lo'.
Diffstat (limited to 'lib/crypto/src/crypto.erl')
-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.