generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Lua Random???

Previous Thread :: Next Thread 
Naco88 is not online. Naco88
Joined: 30 Oct 2009
Total Posts: 665
03 Apr 2017 02:02 PM
So here is my module script:

local module = {}
math.randomseed(os.time())
n = math.random(1,100)
return n

And here is my script:

x = require(game.Workspace.ModuleScript)
print(x)


But every time I run this it prints "96". Even when the seed is os.time???

Can someone tell me how to fix it?


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:03 PM
Call math.random once after seeding the generator and then see if your results are random.


Report Abuse
Naco88 is not online. Naco88
Joined: 30 Oct 2009
Total Posts: 665
03 Apr 2017 02:06 PM
Isn't that what I am doing? Do you mind tweaking the code to show what you mean?


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:08 PM
After seeding the pseudo-random generator, your "random" number returned by the first call to math.random directly after seeding may not necessarily be randomized. If you're curious why you can look it up; I can't really explain it well.

Just do something like this:

math.randomseed(tick())
math.random() -- call the function once after seeding

-- now proceed to do whatever with the function

for i = 1, 10 do
print(math.random())
end


Report Abuse
Nuclear_Energy is online. Nuclear_Energy
Joined: 21 Jul 2013
Total Posts: 1380
03 Apr 2017 02:11 PM
because the module script runs once, so it returns the same value each time it is requested...

I would recommend putting your small module function into your actual script, since the module script is pretty small
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:12 PM
"because the module script runs once, so it returns the same value each time it is requested..."

I overlooked that. That could be the issue but I doubt it. math.random returning the same number each first call is an actual problem.


Report Abuse
Nuclear_Energy is online. Nuclear_Energy
Joined: 21 Jul 2013
Total Posts: 1380
03 Apr 2017 02:17 PM
well, math.random is not really random at all, factor in more "random" factors such as ping or something, so that it will be more random
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:19 PM
Of course it's not random, hence me saying "pseudo-random generator". However, bringing ping into it is unnecessary. All you need is a unique seed such as what tick returns. It's a good seed because it returns the amount of seconds since the Unix epoch on the current machine, and it's a float.


Report Abuse
Nuclear_Energy is online. Nuclear_Energy
Joined: 21 Jul 2013
Total Posts: 1380
03 Apr 2017 02:23 PM
local random_number = 100 --the final result that we use



random_number = random_number * math.sin(time())*0.01


random_number = random_number / math.sin(time())*0.01

random_number = random_number - time()



--anyways, think of ideas to make your numbers more random (even though there is no such thing as random numbers...even in your head, there is a scientific reason why you think of specific numbers while you think of a random number)
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:25 PM
That's even worse than using the built-in math.random function. I don't know why you're so against it but I guarantee it will be more effective than anything anyone can write here.


Report Abuse
Nuclear_Energy is online. Nuclear_Energy
Joined: 21 Jul 2013
Total Posts: 1380
03 Apr 2017 02:29 PM
Players' movement speed
Other real-life influences on the game
The chances of getting an item or map with the usage of a table

These are the factors of random numbers
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 02:34 PM
I still don't understand why you think that using the return value of tick isn't sufficient enough. `tick() % 1 * 1e6` is also used because it varies greatly pretty much every time you call it.


Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
03 Apr 2017 02:47 PM
"Even when the seed is os.time???"
"I still don't understand why you think that using the return value of tick isn't sufficient enough."

tick() is a poor seed because it changes little with respect to time. That is to say unless a sufficient amount of time has past since the last time you've seeded math.random, your need seed 'tick()' will be largely similar to your old seed, yielding similar results.


tick() is fine for seeding online servers because it's not like one person is going to join and leave servers quickly enough to experience this. For things like testing in studio, seeding with tick() can be bothersome if you are only rolling one outcome over a sizable span of time (i.e. picking map). tick()%1*1e7 is my suggested workaround for this 'issue'
Report Abuse
Dominical is not online. Dominical
Joined: 04 Nov 2011
Total Posts: 1303
03 Apr 2017 02:49 PM
local module = {}
function module.getNumber()
math.randomseed(os.time())
n = math.random(1,100)
end
return module


SCRIPT:

x = require(game.Workspace.ModuleScript).getNumber()
print(x)
Report Abuse
Intended_Pun is not online. Intended_Pun
Joined: 10 Aug 2016
Total Posts: 4384
03 Apr 2017 02:49 PM
This is what I use. It seems to be pretty random but I have no idea if it's the best way, it's just a decent way:

string.reverse(tostring(tick()%1*1e6))
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
03 Apr 2017 03:32 PM
module scripts return the last returned item when called on the same host/side (each client and each server)
and try setting it to tick()
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
03 Apr 2017 03:34 PM
"tick() is a poor seed because it changes little with respect to time. That is to say unless a sufficient amount of time has past since the last time you've seeded math.random, your need seed 'tick()' will be largely similar to your old seed, yielding similar results."

Isn't that pretty negligible? I can't say I know how the function in C works but I've never had the issue.


Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
03 Apr 2017 03:36 PM
you can always do tick()*100 if you hate that so much lol
Report Abuse
devHoodie is online. devHoodie
Joined: 04 Nov 2008
Total Posts: 30332
03 Apr 2017 04:12 PM
Different seeds yield different results regardless of distance in numbers to eacher

seed 0 vs seed 1 vs seed 2 have different results

-- Former AKA : luis15232 -- Group discord: /cadXfu3
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image