Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RenderWindow object breaks srand  (Read 4811 times)

0 Members and 1 Guest are viewing this topic.

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
RenderWindow object breaks srand
« 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
srand before RenderWindow

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

code and compile parameters:
(click to show/hide)

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: RenderWindow object breaks srand
« Reply #1 on: April 09, 2017, 02:38:09 pm »

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderWindow object breaks srand
« Reply #2 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.
« Last Edit: April 09, 2017, 02:43:12 pm by dunaevai135 »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: RenderWindow object breaks srand
« Reply #3 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

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.
« Last Edit: April 09, 2017, 03:46:06 pm by Mario »

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderWindow object breaks srand
« Reply #4 on: April 09, 2017, 03:46:47 pm »
As for me, it looks like RenderWindow object initializes srand with one const number

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: RenderWindow object breaks srand
« Reply #5 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)?

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderWindow object breaks srand
« Reply #6 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?

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderWindow object breaks srand
« Reply #7 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()

dunaevai135

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderWindow object breaks srand
« Reply #8 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)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: RenderWindow object breaks srand
« Reply #9 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderWindow object breaks srand
« Reply #10 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.
Laurent Gomila - SFML developer

 

anything