Skip to content

Commit

Permalink
Generate only as many bytes as necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrsagen committed Jul 12, 2017
1 parent 3128451 commit 07f95ef
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "GarrysMod/Lua/Interface.h"
#include <random>
#include <string>
#include <climits>
#include <vector>

int randomNumber(lua_State* state) {
std::random_device rd;
std::mt19937 gen(rd());
gen.discard(700000); // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/lfsr04.pdf page 11

if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) {
LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER);
Expand All @@ -30,14 +32,15 @@ int randomBytes(lua_State* state) {
}

std::random_device rd;
std::vector<unsigned int> bytes;
std::uniform_int_distribution<int> dist(SCHAR_MIN, SCHAR_MAX);
std::vector<char> bytes;
bytes.resize(size);

for (size_t i = 0; i < size; i++) {
bytes[i] = rd();
bytes[i] = (char)dist(rd);
}

LUA->PushString((const char*)bytes.data(), size);
LUA->PushString(bytes.data(), size);

return 1;
}
Expand Down

0 comments on commit 07f95ef

Please sign in to comment.