Skip to content

LuaForWindows_v5.1.4-46 #131

Open
Open

Description

@mahdi-hajian

math.random() is wrong

for example
math.random(200)
is only 1
or
math.random(200,300)
is only 200

Activity

  1. mahdi-hajian commented on Dec 11, 2020

    @mahdi-hajian
    Author

    in last version is wrong too
    Lua for Windows v5.1.5-52 Released

  2. TFWol commented on Mar 6, 2023

    @TFWol

    That's a lua problem.

    -- you only get 200
    count = 0
    while count < 100 do
      number = math.random(200, 300)
      print(number)
      if number == 200 then
        break
      end
      count = count + 1
    end
    -- works properly
    math.randomseed(os.time())
    
    count = 0
    while count < 100 do
      number = math.random(200, 300)
      print(number)
      if number == 200 then
        break
      end
      count = count + 1
    end

    REF:
    https://stackoverflow.com/questions/18199844/lua-math-random-not-working

  3. JJSax commented on Dec 31, 2024

    @JJSax

    This issue is more than a randomseed issue.

    print(os.time()) -- this prints a different time each time. 
    math.randomseed(os.time())
    for i = 1, 5 do 
      print(math.random(1, 100)) 
    end

    outputs this, notice the first number on every test. Each test was done several seconds apart from each other to ensure os.time() returned a new number.

    -- test 1
    42
    35
    28
    93
    33
    -- test 2
    42
    68
    82
    66
    65
    -- test 3
    42
    34
    91
    13
    28
    -- test 4
    42
    66
    46
    87
    59
    -- test 5
    42
    99
    100
    60
    91
    -- test 6
    42
    65
    9
    7
    54
    -- test 7
    42
    98
    64
    80
    85
    -- test 8
    42
    30
    18
    54
    17
    -- test 9
    42
    62
    36
    48
    43
    

    Running the same code on several online compilers this issue doesn't happen. Those compilers use Lua 5.4, one allowed me to use down to lua 5.3,2 and the issue was not a thing with that one either.

    It's possible this issue existed in Lua 5.1 which this project uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions