This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

math.random() allways retuns 0.7942
#1
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!!
Reply
#2
See this: https://forum.logicmachine.net/showthread.php?tid=2052
Reply
#3
(03.02.2023, 11:32)admin Wrote: See this: https://forum.logicmachine.net/showthread.php?tid=2052

Thanks! It works!
Reply
#4
Can I have the math.random function output random numbers, letters, and special characters?
Reply
#5
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)
Reply
#6
(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
Reply


Forum Jump: