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

Author Topic: [Beginner] Help with Bouncing Circles  (Read 51530 times)

0 Members and 4 Guests are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #90 on: February 15, 2014, 05:16:47 am »
It looks like you keep resetting the seed for the random function. Take a look at the <random> part of the stl. IT has a lot better functionality than the old random.
Okay, I now I use the new random:
default_random_engine engine;
uniform_int_distribution<int> distribution(1,255);
auto random = bind ( distribution, engine );
 
I use this no matter which border is touched:
Ball[i].setFillColor(sf::Color(random(), random(), random()));
 

Now it definetely looks better, but do I need to set an extra seed for the generator?

The seed is to make the random generator behave differently on each run. If you did not set a seed, then the results would be the same each time you start the program. The balls would change colors in the same sequence every time the program is ran. That is the purpose of the seed, to prevent that.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #91 on: February 15, 2014, 09:43:17 am »
The seed is to make the random generator behave differently on each run. If you did not set a seed, then the results would be the same each time you start the program. The balls would change colors in the same sequence every time the program is ran. That is the purpose of the seed, to prevent that.
Yeah, I know what a seed does, but I didn't know if the new random needs a seed too, or if, for example, the system time is automatically used as seed.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #92 on: February 15, 2014, 10:11:33 am »
You should seed it.
And the system time is a terrible seed. Use std::random_device to get a good seed value.
See: http://en.cppreference.com/w/cpp/numeric/random
« Last Edit: February 15, 2014, 10:16:48 am by Jesper Juhl »

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #93 on: February 15, 2014, 11:06:26 am »
Okay, I made two different engines with two different seeds. But when I try to use an number created from random_device, it compiles fine, but when I start the program, it closes immediately.

Here's some code:

        random_device seedDevice;
        uniform_int_distribution<int> seedDist(0, 1000000);
       
        default_random_engine engine(seedDist(seedDevice));
        uniform_int_distribution<int> distribution(1,255);
        auto random = bind ( distribution, engine );

        default_random_engine engine2(seedDist(seedDevice));
        auto random2 = bind (distribution, engine2);
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Beginner] Help with Bouncing Circles
« Reply #94 on: February 15, 2014, 02:55:07 pm »
And the system time is a terrible seed.
Why?

Use std::random_device to get a good seed value.
std::random_device is not guaranteed to use a hardware random source. It may fall back to a deterministic pseudo-number generator, and then you're doing even worse than with the system time.

You should really not make your life too complicated if you're just using random numbers for games. Mostly, you won't even notice the problems of LCGs (using C++11 random engines is still a good idea).

But when I try to use an number created from random_device, it compiles fine, but when I start the program, it closes immediately.
What does that mean? Find out with the debugger where the program closes. More useful than "some code" would also be a minimal complete example.

And something important: Please open new threads for new problems. This one is already far too long, and the current discussion has nothing to do with the original problem.
« Last Edit: February 15, 2014, 03:02:29 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #95 on: February 15, 2014, 03:03:26 pm »
An example:

#include <random>
#include <functional>
#include <iostream>
#include <cstdlib>

int main()
{
    std::random_device seedDevice;
    std::default_random_engine engine(seedDevice());
    std::uniform_int_distribution<int> distribution(1, 255);
    auto random = std::bind(distribution, std::ref(engine));
    std::cout << "Generating 10 random numbers" << std::endl;
    for (int i = 0; i < 10; ++i)
        std::cout << "\tRandom number " << i << ": " << random() << std::endl;
    return EXIT_SUCCESS;
}
 

Sample output:

$ g++ -std=c++11 test.cc
$ ./a.out
Generating 10 random numbers
        Random number 0: 53
        Random number 1: 136
        Random number 2: 85
        Random number 3: 210
        Random number 4: 109
        Random number 5: 50
        Random number 6: 201
        Random number 7: 195
        Random number 8: 62
        Random number 9: 124

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #96 on: February 15, 2014, 03:09:29 pm »
And the system time is a terrible seed.
Why?

For a game it is probably fine.
In general it's a way too guessable value. An attacker with knowledge of the fact that you are using the system time as a seed (regardless of resolution) has just narrowed the scope, of the numbers he has to guess in order to guess your seed, greatly.

Use std::random_device to get a good seed value.
std::random_device is not guaranteed to use a hardware random source. It may fall back to a deterministic pseudo-number generator, and then you're doing even worse than with the system time.

True, so for something other than a game I guess you'd want to mix up a few sources; including random_device, system time and more.

You should really not make your life too complicated if you're just using random numbers for games. Mostly, you won't even notice the problems of LCGs (using C++11 random engines is still a good idea).

True.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #97 on: February 15, 2014, 04:51:37 pm »
After all I just use the system time as seed :)

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #98 on: February 15, 2014, 05:52:23 pm »
The seed is to make the random generator behave differently on each run. If you did not set a seed, then the results would be the same each time you start the program. The balls would change colors in the same sequence every time the program is ran. That is the purpose of the seed, to prevent that.
Yeah, I know what a seed does, but I didn't know if the new random needs a seed too, or if, for example, the system time is automatically used as seed.

The seed is just the starting point. Random number generators work off of the previous value it generated to make a number.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #99 on: February 21, 2014, 12:38:38 am »
And something important: Please open new threads for new problems. This one is already far too long, and the current discussion has nothing to do with the original problem.

Okay, I understand that this thread is too long... Currently I have two problems with my project, I'll open new threads for them.

This thread can be closed.

 

anything