SFML community forums

General => General discussions => Topic started by: dunaevai135 on April 08, 2017, 08:01:54 pm

Title: RenderWindow object breaks srand
Post by: dunaevai135 on April 08, 2017, 08:01:54 pm
When
srand((unsigned)time(NULL));
called before RenderWindow class object creation, rand() return same value, like srand() initialize by const var.
If i will put srand after RenderWindow creation, everything OK.
see attached screenshot

I'm confused that the example "examples/pong/Pong.cpp" uses a similar code:
https://github.com/SFML/SFML/blob/fae3b65f0567f87fa9925cd42d28df15eb69e79c/examples/pong/Pong.cpp (https://github.com/SFML/SFML/blob/fae3b65f0567f87fa9925cd42d28df15eb69e79c/examples/pong/Pong.cpp)
srand before RenderWindow

P.S. sorry for my English, it's my second language.

code and compile parameters:
(click to show/hide)
Title: Re: RenderWindow object breaks srand
Post by: Turbine on April 09, 2017, 02:38:09 pm
I don't think you can use srand in a loop. Non-SFML related.

http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c (http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c)
Title: Re: RenderWindow object breaks srand
Post by: dunaevai135 on April 09, 2017, 02:41:13 pm
see screenshot
srand out of loop before RenderWindow initialise
it only once, at the start of main()
If i will put srand after RenderWindow creation, everything OK.
Title: Re: RenderWindow object breaks srand
Post by: Mario on April 09, 2017, 03:43:24 pm
I don't think you can use srand in a loop. Non-SFML related.

http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c (http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c)

You can. The only problem is, time() has limited resolution, so if you loop fast enough, you'll start with the same seed for several iterations, therefore returning the samepseudo-random number every time for a while.

As for initial problem: Not sure how SFML would influence srand()/rand().

Edit:

Just tried the code and I get random/different numbers every time I run the example snippet. Everything fine. Could you also print your seed, i.e. the return value of time()? I have the suspicion there's something off with your system for whatever reason.
Title: Re: RenderWindow object breaks srand
Post by: dunaevai135 on April 09, 2017, 03:46:47 pm
As for me, it looks like RenderWindow object initializes srand with one const number
Title: Re: RenderWindow object breaks srand
Post by: Mario on April 09, 2017, 03:47:55 pm
Which version of SFML are you running? There's no call to "srand()" in SFML's source.

Edit:
Also, what happens if you create a second sf::RenderWindow and run the loop again (without calling srand() a second time)?
Title: Re: RenderWindow object breaks srand
Post by: dunaevai135 on April 09, 2017, 04:01:25 pm
I tried the latest stable version
code:
(click to show/hide)
out:
(click to show/hide)

the same problem
putting srand() after RenderWindow app(vdm, "Game!"); repairs for me.

It can be bag?

$ uname -a
Linux dunaev-desktop 4.4.0-71-generic #92-Ubuntu SMP Fri Mar 24 12:59:01 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
 
What else about the system to provide?
Title: Re: RenderWindow object breaks srand
Post by: dunaevai135 on April 09, 2017, 04:06:19 pm
It really strange
see :

code:
(click to show/hide)

out:
(click to show/hide)

like
RenderWindow app(vdm, "Game!");
reinitializes srand()
Title: Re: RenderWindow object breaks srand
Post by: dunaevai135 on April 09, 2017, 04:10:40 pm
Which version of SFML are you running? There's no call to "srand()" in SFML's source.

Edit:
Also, what happens if you create a second sf::RenderWindow and run the loop again (without calling srand() a second time)?

(click to show/hide)

out:
(click to show/hide)
Title: Re: RenderWindow object breaks srand
Post by: eXpl0it3r on April 09, 2017, 04:27:52 pm
How about you forget srand and rand and use some proper RNG in the <random> header introduced with C++11.

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Title: Re: RenderWindow object breaks srand
Post by: Laurent on April 09, 2017, 08:10:12 pm
Quote
As for me, it looks like RenderWindow object initializes srand with one const number
A quick search in source code of master shows that the only call to srand is in the Pong example. So if SFML does something, it's hidden in one of its dependencies.

I really suspect something in your environment. Definitely not SFML itself.