Advertisement
fuxoft

Generating random binary number with X ones

Feb 1st, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.27 KB | None | 0 0
  1.         local ones = 13 + rnd(5) --Generating number with 14, 15, 16, 17 or 18 ones
  2.         for bitn = 32, 1, -1 do
  3.             local got = 0
  4.             if rnd(bitn) <= ones then
  5.                 got = 1
  6.             end
  7.             ones = ones - got
  8.             num = bit.rol(bit.bor(num, got), 1)
  9.         end
  10.         print("num", bit.tohex(num))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement