Logic Machine Forum
math.random() allways retuns 0.7942 - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: math.random() allways retuns 0.7942 (/showthread.php?tid=4554)



math.random() allways retuns 0.7942 - kike - 03.02.2023

Hi, 

I noticed that when I use the math.random() with out arguments returns allways the same value, if arguments are used return the maximun.

example: 

log(math.random())


returns:

* number: 0.794206292431241

what I am doing wrong?

Thanks!!


RE: math.random() allways retuns 0.7942 - admin - 03.02.2023

See this: https://forum.logicmachine.net/showthread.php?tid=2052


RE: math.random() allways retuns 0.7942 - kike - 03.02.2023

(03.02.2023, 11:32)admin Wrote: See this: https://forum.logicmachine.net/showthread.php?tid=2052

Thanks! It works!


RE: math.random() allways retuns 0.7942 - Hoang Hien - 01.06.2024

Can I have the math.random function output random numbers, letters, and special characters?


RE: math.random() allways retuns 0.7942 - admin - 03.06.2024

Use this. Modify the character list as needed.
Code:
function randompassword(length)
  local characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  local max = #characters - 1
  local buffer = {}

  length = length or 12

  local ts, tu = os.microtime()
  math.randomseed(ts * 1e6 + tu)

  for i = 1, length do
    local rnd = math.random(1, max)
    buffer[ i ] = characters:sub(rnd, rnd)
  end

  return table.concat(buffer)
end

length = 8
password = randompassword(length)
log(password)



RE: math.random() allways retuns 0.7942 - Hoang Hien - 03.06.2024

(03.06.2024, 06:40)admin Wrote: Use this. Modify the character list as needed.
Code:
function randompassword(length)
  local characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  local max = #characters - 1
  local buffer = {}

  length = length or 12

  local ts, tu = os.microtime()
  math.randomseed(ts * 1e6 + tu)

  for i = 1, length do
    local rnd = math.random(1, max)
    buffer[ i ] = characters:sub(rnd, rnd)
  end

  return table.concat(buffer)
end

length = 8
password = randompassword(length)
log(password)

thank you very much