how can i get a realy random value in the abm ?

User avatar
tkerwel
Member
 
Posts: 213
Joined: Fri Jan 13, 2012 07:35

how can i get a realy random value in the abm ?

by tkerwel » Wed Jul 25, 2012 11:58

when i use this code, every time the abm is runing i got the this values...

the values 73,74,75,76,78,79


minetest.register_abm({
nodenames = { "deko:deko_birch_sapling" },
interval = 120,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
-
math.randomseed(os.time())
max_height = math.random(1,100)

print('[Deko Mod] Birch Mod 06/12 Loaded max_height!=> '.. max_height )


end

i want here a number between 3 and 8, using math.random(3,8) istnt random...every time the same values.

so to my understanding i should with math.randomseed(os.time()) a random seed for the math.random

any idea how i can get this a bit more random ?
++++ Kung walang tiyaga, walang nilaga. ++++
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Jul 25, 2012 12:30

PseudoRandom: A pseudorandom number generator
- Can be created via PseudoRandom(seed)
methods:
- next(): return next integer random number [0...32767]
- next(min, max): return next integer random number [min...max]
(max - min) must be 32767 or <= 6553 due to the simple
implementation making bad distribution otherwise.

From doc/init.lua
 

User avatar
tkerwel
Member
 
Posts: 213
Joined: Fri Jan 13, 2012 07:35

by tkerwel » Wed Jul 25, 2012 12:44

so to make it sure...

next (1,6) and math.random(1,6) are doing the same thing ?

for my code i can use

minetest.register_abm({
nodenames = { "deko:deko_birch_sapling" },
interval = 120,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
-
math.randomseed(os.time())
max_height = math.random(1,100) or max_height =next (1,100)


print('[Deko Mod] Birch Mod 06/12 Loaded max_height!=> '.. max_height )

end

for my understanding ...minetest has his own pseudorandom number generator i use with next() in lua scripts within minetest ?
++++ Kung walang tiyaga, walang nilaga. ++++
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Jul 25, 2012 13:03

you have to create a PseudoRandom first with
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local rand =  PseudoRandom(seed)
and then you can use
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
rand:next(1, 100)

But with this method you also have to set a seed.
When I use math.random() without a seed there are different numbers each time.
 

User avatar
tkerwel
Member
 
Posts: 213
Joined: Fri Jan 13, 2012 07:35

by tkerwel » Wed Jul 25, 2012 13:14

thx for the help...

the main mistake was that i always call=> math.randomseed(os.time()) in the abm, removed that and now its working.

but i still use math.randomseed(os.time()) and math.random (min, max) from lua... guess when i am done i try it with the next() to see if the random generator is better.
++++ Kung walang tiyaga, walang nilaga. ++++
 


Return to Minetest Problems

Who is online

Users browsing this forum: Bing [Bot] and 6 guests

cron