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
Quick Lua math question
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Quick Lua math question
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Quick Lua math question
Code: Select all
function magic(n)
return tonumber(tostring(n):gsub("[^%d]", ""))
end
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Quick Lua math question
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)
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Quick Lua math question
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.
Re: Quick Lua math question
Oh right. The problem is that gsub returns more than one variable, which is then fed to tonumber. We don't want that.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)
Code: Select all
function magic(n)
return tonumber((tostring(n):gsub("[^%d]", "")))
end
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Quick Lua math question
Robin, I know. However, it's not good enough. Here's what math.random generates for me:
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?
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Quick Lua math question
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.
Actually, you should go to irc.freenode.net/#lua and ask for help with your random number stuff yourself. That'll be more helpful.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Quick Lua math question
- @
Why can't we just use math.floor instead?thelinx wrote:Code: Select all
function magic(n) return tonumber((tostring(n):gsub("[^%d]", ""))) end
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?Taehl wrote:Doesn't look very random, does it? Hence, I need to write my own.
When I write def I mean function.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Quick Lua math question
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:
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?
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
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Quick Lua math question
What's wrong with using this?
Using high numbers as argument to math.sinh() will overflow the number and only return nan.
Code: Select all
function math.drand(seed, min, max)
math.randomseed(seed)
math.random(min, max)
end
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests