udcrr.cpp (1041B)
1 #include <cstdio> 2 #include <random> 3 4 #ifdef LUA 5 #include <lua.hpp> 6 #endif 7 8 #include "udcrr.data.hpp" 9 10 template <typename T, std::size_t N> 11 constexpr static std::size_t ArrayLen(T (&)[N]) noexcept { return N; } 12 13 static std::random_device rd; 14 static std::uint32_t Rand(std::uint32_t max) noexcept 15 { 16 std::uniform_int_distribution<std::uint32_t> dist(0, max-1); 17 return dist(rd); 18 } 19 20 static void gen_id(char* buf, std::size_t len) 21 { 22 auto adv = ADVERBS[Rand(ArrayLen(ADVERBS))]; 23 auto adj = ADJECTIVES[Rand(ArrayLen(ADJECTIVES))]; 24 auto noun = NOUNS[Rand(ArrayLen(NOUNS))]; 25 auto verb = VERBS[Rand(ArrayLen(VERBS))]; 26 auto num = Rand(10000); 27 28 std::snprintf(buf, len, "%s-%s-%s-%s-%04d", adv, adj, noun, verb, num); 29 }; 30 31 #ifdef LUA 32 static int lua_gen_id(lua_State* vm) 33 { 34 char buf[256]; 35 gen_id(buf, 256); 36 lua_pushstring(vm, buf); 37 return 1; 38 } 39 40 extern "C" int luaopen_id_gen(lua_State* vm) 41 { 42 lua_pushcfunction(vm, lua_gen_id); 43 return 1; 44 } 45 #else 46 int main() 47 { 48 char buf[256]; 49 gen_id(buf, 256); 50 puts(buf); 51 return 0; 52 } 53 #endif