Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Strange WIDTH vs HEIGHT bug.

edited August 2018 in Bugs Posts: 4

I noticed a strange bug when working with math.random in Codea.

The following code fails with an error: “bad argument #1 to ‘random’ (number has no integer representation)”


This code works without an issue.


Any ideas?


P.S. I am on the developer beta of iOS if that matters. Running latest Codea release.



  • edited August 2018 Posts: 1,098

    @GimbalLock - interesting, repeated your code with same result, tried;

    function setup() -- print(WIDTH.." "..type(WIDTH)) test1 = math.random()*WIDTH/2 print("Test 1 : "..test1) test2 = math.random(768) print("Test 2 : "..test2) print(HEIGHT.." "..type(HEIGHT)) test3 = math.random()*HEIGHT/2 print(math.random(HEIGHT/2)) print("Test 3 : "..test3) end

    No errors, it would seem that at some stage in the Codea code WIDTH is converted to a decimal number before encountering the print statement.

    Edit: updated code, thinking about it the problem seems to be with HEIGHT as any random number should be a real number. I believe the approved way of using math.random() is now math.random()*variable. Perhaps HEIGHT has been rounded to an integer but not WIDTH within the Codea random function, making the assumption that if you are randomising the screen variable an integer is required.

    p.s. does type() differentiate between number and integer?

  • dave1707dave1707 Mod
    Posts: 7,312

    math.random requires an integer as an input. When you divide WIDTH by 2, the result might be a fraction depending on the value of WIDTH. To make sure that you always get an integer when you need it, use the integer divide // ( WIDTH//2 ) instead of / ( WIDTH/2 ) .

  • Posts: 1,098

    @dave1707 - good point, when the side panel was up, portrait mode, to print the values the width on my iPad was 493, hence the non-integer error report.

  • @Bri_G @dave1707 Ah-ha! That’s it. Thanks for the help. It had to be something simple like that.

Sign In or Register to comment.