Quick Lua math question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Quick Lua math question

Post by Taehl »

C's pseudorandom number generator isn't working quite like I need, so I'm making my own. So, in Lua, how can I remove the decimal point from a number? For example:

magic(8.1) = 81
magic(0.3333333) = 3333333
magic(11.180339887499) = 11180339887499

If that's not possible (or fast), alternately, how can I get just the decimal of a number as an integer?

magic2(14.56699) = 56699
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Quick Lua math question

Post by thelinx »

Code: Select all

function magic(n)
  return tonumber(tostring(n):gsub("[^%d]", ""))
end
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Quick Lua math question

Post by Taehl »

Ah, thank you. You're very helpful, thelinx. I'll do some tests and see if I can get nicer-looking pseudorandom numbers with this...

EDIT) Wait, that doesn't work... I get the error
stdin:1: bad argument #2 to 'tonumber' (base out of range)
Last edited by Taehl on Wed Nov 10, 2010 8:33 pm, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Quick Lua math question

Post by Robin »

What kind of numbers do you need? Integers? You do know you can just use math.random(maximum), right? (Just checking :))
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Quick Lua math question

Post by thelinx »

Taehl wrote:Ah, thank you. You're very helpful, thelinx. I'll do some tests and see if I can get nicer-looking pseudorandom numbers with this...

EDIT) Wait, that doesn't work... I get the error
stdin:1: bad argument #2 to 'tonumber' (base out of range)
Oh right. The problem is that gsub returns more than one variable, which is then fed to tonumber. We don't want that.

Code: Select all

function magic(n)
  return tonumber((tostring(n):gsub("[^%d]", "")))
end
Note the extra set of parentheses.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Quick Lua math question

Post by Taehl »

Robin, I know. However, it's not good enough. Here's what math.random generates for me:
Image

Doesn't look very random, does it? Hence, I need to write my own.

EDIT) thelinx, that seems to work. Now I'll see what I can produce with it. That code isn't slow, is it?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Quick Lua math question

Post by thelinx »

After testing, it does seem to cause quite an overhead. I don't know any other way though. I'll look around.

Actually, you should go to irc.freenode.net/#lua and ask for help with your random number stuff yourself. That'll be more helpful.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Quick Lua math question

Post by kikito »

  • @
    thelinx wrote:

    Code: Select all

    function magic(n)
      return tonumber((tostring(n):gsub("[^%d]", "")))
    end
    Why can't we just use math.floor instead?
    Taehl wrote:Doesn't look very random, does it? Hence, I need to write my own.
    It does look as if you were using math.random the wrong way. Can I see the code you are using for math.random, and your expected results?
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Quick Lua math question

Post by Taehl »

Basically, kikito, it's a procedural-generation thing. I have a really interesting idea for truly unique procedural world detailing, however, it requires that I can make a deterministic chain of pseudorandom numbers for any given coordinate. So what that picture is is me feeding the tile coordinates to math.randomseed and choosing a tile at math.random. Not so ideal...

Another question: Do this in the Lua console:

Code: Select all

function math.drand(seed, min, max) return math.sinh(seed) %(max-min)+min end
for i=0,999 do if math.drand(i, 0,3) > 3 then print(i, math.drand(i, 0,3)) end end
Presumably, putting the number through %(max-min)+min should force it to in the range of max>=n>=min, right? Well, watch your console spawn dozens of impossible answers... What the hell is going on? :?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Quick Lua math question

Post by vrld »

What's wrong with using this?

Code: Select all

function math.drand(seed, min, max) 
    math.randomseed(seed)
    math.random(min, max)
end
Using high numbers as argument to math.sinh() will overflow the number and only return nan.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests