randomseed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

randomseed

Post by elsalvador »

is this the right way to use randomseed?????????
i still don't get random seed :(
or am i missing something else???

Code: Select all


function love.load()
	g =love.graphics	
	f1= g.newFont('BulwarkNF.ttf',20)
	f2= g.newFont(20)
	math.randomseed(os.time())	
end
function love.draw()	
	g.setFont(f1)
	g.setColor(255,255,255,255)
	g.print('my new Random number!',10,10)
	
	g.setFont(f2)
	g.setColor(25,255,25,255)
	g.print('Regular Number',10,50)
	g.print('random number: '..(math.random(1, 10)),10,100)
end

BTW i love the forums you guys are the best :) :awesome:
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: randomseed

Post by Plu »

You don't have to use randomseed, it's called automatically by the löve startup. But yes, that would be the right way to call it; it's just not neccesary in löve programs.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:03 am, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: randomseed

Post by micha »

bekey wrote:It has been advised before in the forums to call "math.random()" at least 3 times initially, best likely at the start of the whole game.
I have read this somewhere, too, but it is incorrect.

Just don't worry about seeds and use math.random() without any extra stuff.

(The only exception is, when you need reproducible random numbers).
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: randomseed

Post by Boolsheet »

It's not incorrect. It's meant to warm up the generator. Depending on the algorithm behind math.randomseed and math.random, the first random value may be very closely related to the seed. This can trip you up if you expect an actual random first number and instead get slowly increasing numbers because you seed from time. Three steps into it will mix the seed up to a point where we hopefully don't have that effect anymore.
Shallow indentations.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: randomseed

Post by DaedalusYoung »

Especially on Mac OS X and Linux, there appear to be issues with math.random. And apparently, you get different values per OS. Which is why we'll be getting a prng in LÖVE 0.9.0, love.math.random.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: randomseed

Post by micha »

Boolsheet wrote:It's not incorrect. It's meant to warm up the generator. Depending on the algorithm behind math.randomseed and math.random, the first random value may be very closely related to the seed. This can trip you up if you expect an actual random first number and instead get slowly increasing numbers because you seed from time. Three steps into it will mix the seed up to a point where we hopefully don't have that effect anymore.
First, if you are constantly seeding from time, then you use the random generator in a wrong way. The idea is to seed it once and then just use it.

Second, nevertheless I doubt, that seeding from two similar seeds will produce similar random numbers. Not even the first one. So even if you seeded from time constantly, and you made sure you don't seed with the same number then drawing three random numbers before the actual use would not increase the quality of the random numbers.

And to be specific. I am talking about an implementation of rand like this:

Code: Select all

next = next * 1103515245 + 12345;
return (UINT32)(next>>16) & RAND_MAX;
(I got it from Stackoverflow), which is a linear congruential generator. Seeding in this generator means that you set the variable "next" to a specific value. When rand is called then first some calculatation are done on "next" and then an output is generated. Even if two seeds only differ by 1, the new values of "next" differ by 1,103,515,245 and then a bit shift is done which puts the front digits to the back and vice versa.

But maybe I missed something. Can you provide an example or specify what you mean by "this can trip you up"?
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: randomseed

Post by vrld »

micha wrote:But maybe I missed something. Can you provide an example or specify what you mean by "this can trip you up"?
You argumentation is correct. There should be no "visible pattern" after seeding with os.time(), unless you seed more than once per second. However, on OSX (and probably other BSD's too) there is a bug on the C side of the implementation (or rather the interplay between C and Lua). More details can be found on this page (scroll to math.random).

The bug can be squished by seeding with math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) ). As mentioned, another solution is to call math.random() after seeding. One time should be sufficient, though.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: randomseed

Post by Boolsheet »

The beauty of the C runtime library is that everyone has its own implementation. Here's the assembly from the MS VC++ 2010 x86 runtime library.

Code: Select all

mov     eax,dword ptr [ecx+14h]
imul    eax,eax,343FDh
add     eax,269EC3h
mov     dword ptr [ecx+14h],eax
shr     eax,10h
and     eax,7FFFh
ret
Which is indeed very similar to ANSI C: Watcom, Digital Mars, CodeWarrior, IBM VisualAge C/C++ (according to Wikipedia).

An example on Windows.

Code: Select all

> t = os.time()
> print(t)
1385036675
> math.randomseed(t) for i=1, 3 do print(math.random() * (2^15-1)) end
12756
17222
21135
> math.randomseed(t+1) for i=1, 3 do print(math.random() * (2^15-1)) end
12759
27971
6231
> math.randomseed(t+2) for i=1, 3 do print(math.random() * (2^15-1)) end
12763
5951
24095
> math.randomseed(t+3) for i=1, 3 do print(math.random() * (2^15-1)) end
12766
16699
9191
I agree that there's no issue with it being "more or less random". It's just unexpected and people get annoyed by that. :P
Shallow indentations.
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: randomseed

Post by elsalvador »

WOW..
a lot of info ( i am new still super new) So...
the way I did it was it good or not???? because my game will depend on the math.random() but than idk how to use it if this is not the right way to use it?
In my game I need to have always a random numbers to trigger an action .. :(
can someone give me the code already doing a math.random() and also where to place it!! i don't want my game to crash or annoy people :( in the game..

( IF can't use randomseed) then
math.random(0,1000) <---------that's how max my number will be so if ends in 999 will trigger something etc...
now how to use it???? will it goes under update? or load? where???? :(
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests