|
3 | 3 | import dan200.computercraft.api.lua.IArguments; |
4 | 4 | import dan200.computercraft.api.lua.LuaException; |
5 | 5 | import dan200.computercraft.api.lua.LuaFunction; |
| 6 | +import dan200.computercraft.api.lua.LuaValues; |
6 | 7 | import dan200.computercraft.api.peripheral.IPeripheral; |
7 | 8 | import me.alexdevs.classicPeripherals.core.Crypto; |
8 | 9 | import me.alexdevs.classicPeripherals.utils.CryptoUtils; |
@@ -62,17 +63,25 @@ public final String hmacSha512(String key, String data, Optional<Boolean> hex) { |
62 | 63 |
|
63 | 64 | @LuaFunction |
64 | 65 | public final double random(IArguments args) throws LuaException { |
65 | | - var value = Crypto.secureRandom(); |
66 | | - |
67 | 66 | if (args.count() == 0) { // 0.0 - 1.0 |
68 | | - return value; |
| 67 | + return Crypto.secureRandom(); |
69 | 68 | } else if (args.count() == 1) { // 0.0 - max |
70 | | - var max = args.getInt(0); |
71 | | - return Math.floor(value * max); |
| 69 | + var max = args.getFiniteDouble(0); |
| 70 | + |
| 71 | + if (max < 0) { |
| 72 | + throw new LuaException("bad argument #1 (interval is empty)"); |
| 73 | + } |
| 74 | + |
| 75 | + return Crypto.secureRandom(max); |
72 | 76 | } else { // min - max |
73 | | - var min = args.getInt(0); |
74 | | - var max = args.getInt(1); |
75 | | - return Math.floor(value * max) + min; |
| 77 | + var min = args.getFiniteDouble(0); |
| 78 | + var max = args.getFiniteDouble(1); |
| 79 | + |
| 80 | + if (max - min <= 0) { |
| 81 | + throw new LuaException("bad argument #1 (interval is empty)"); |
| 82 | + } |
| 83 | + |
| 84 | + return Crypto.secureRandom(min, max); |
76 | 85 | } |
77 | 86 | } |
78 | 87 |
|
|
0 commit comments