16

I need to generate random float in Lua. It needs to be > 1, so math.random() is not a solution.

How can I do this?

fresskoma
  • 25,481
  • 10
  • 85
  • 128
user0103
  • 1,246
  • 3
  • 18
  • 36

3 Answers3

14

This should generate random floats between 1 (inclusive) and 100 (exclusive)

math.random() + math.random(1, 99)
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
peacemaker
  • 2,541
  • 3
  • 27
  • 48
12

You can also use something like this to get a number between lower and greater

function randomFloat(lower, greater)
    return lower + math.random()  * (greater - lower);
end
Stals
  • 1,543
  • 4
  • 27
  • 52
3

Just posting for fun, but you can use math.random() with no arguments to do this :P

print(math.floor((math.random()*100)+0.5))
Nathan
  • 430
  • 5
  • 7