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

Author Topic: Random number generator  (Read 10214 times)

0 Members and 1 Guest are viewing this topic.

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Random number generator
« on: June 07, 2011, 01:39:13 pm »
Hi,
  I am trying to get random number generated by using sf::Randomizer
but every time i am getting the same number. when i run the program.

my code is
Code: [Select]

Randomizer1(float min,float max)
{
sf::Randomizer::SetSeed((unsigned int)time(NULL));
float value = sf::Randomizer::Random(min, max);

return value;
}


Random(int min, int max)
{
srand((unsigned)time(0));
int val = min + rand() % max;

return val;
}



its first i will use Random() then Randomizer like this
Code: [Select]

int min = Random(1, 5);
int max = Random(6,10);
float result = Randomizer1((float) min, (float) max);


Why is it happening??....i am trying to build a particle engine. For that i am using this function to get random values for the velocity/energy etc for each particle.


All suggestions are welcome

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Random number generator
« Reply #1 on: June 07, 2011, 01:42:20 pm »
You don't need to call sf::Randomizer::SetSeed() because SFML initializes the random number generator automatically. Only call it when you want the same sequence at every program startup. std::srand() should be called only once. Note that it affects the state of SFML's generator, so don't call std::srand() if you use it.

The sf::Randomizer class has been removed from SFML 2, so I wouldn't use it anymore, at the latest if you update to a new SFML version.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Random number generator
« Reply #2 on: June 07, 2011, 01:49:37 pm »
Quote from: "Nexus"
You don't need to call sf::Randomizer::SetSeed() because SFML initializes the random number generator automatically. Only call it when you want the same sequence at every program startup. std::srand() should be called only once. Note that it affects the state of SFML's generator, so don't call std::srand() if you use it.

The sf::Randomizer class has been removed from SFML 2, so I wouldn't use it anymore, at the latest if you update to a new SFML version.


I am using sfml 1.6 for time being. I want to know whether if i remove the sf::Randomizer::SetSeed() and srand(), then is it a good random number generation for the particle engine???

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Random number generator
« Reply #3 on: June 07, 2011, 01:53:21 pm »
SFML uses std::rand() internally, which is a Linear Congruential Generator. This is not the best one available, but good enough for many purposes. For my particle system, I use an custom generator, but it is also possible to work with TR1.Random or Boost.Random if the standard library implementation doesn't suffice.

You must remove std::srand() and sf::Randomizer::SetSeed() from the random calls in order to achieve uniformly distributed random variables.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Random number generator
« Reply #4 on: June 07, 2011, 02:03:48 pm »
Quote from: "Nexus"
SFML uses std::rand() internally, which is a Linear Congruential Generator. This is not the best one available, but good enough for many purposes. For my particle system, I use an custom generator, but it is also possible to work with TR1.Random or Boost.Random if the standard library implementation doesn't suffice.

You must remove std::srand() and sf::Randomizer::SetSeed() from the random calls in order to achieve uniformly distributed random variables.

After removing those function it is some what working according to the requirement.
Can you give a brief idea about the custom generator which you have developed.
If its not a inconvenience to you can I send you the structure of my Particle engine, so that you can give me your valuable suggestions on it.
That will be a very great help for me :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Random number generator
« Reply #5 on: June 07, 2011, 02:23:25 pm »
My library uses a Multiply-With-Carry generator. The code is open-source and documented, so you can have a look at it or even use it directly ;)

When you want to, post the important parts of your structure here (please minimize unrelated and irrelevant code, ideal would be a semantic overview). If it helps you, you can also look at the structure of my particle system, it is explained in a tutorial. The SVN repository of my library is available at http://www.bromeon.ch/svn/thor/trunk and everything else can be found here :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Random number generator
« Reply #6 on: June 07, 2011, 02:50:01 pm »
my particle engine have a structure like
Code: [Select]

                          ParticleManager
                                   |
                          ParticleSystem
                                   |
                   --------------------------------
                   |                 |                     |
                Smoke        Blast        .....    Other
            |       |
    Particle     Particle

What i am trying is when i need some effect i will call like
Code: [Select]

Blast(number of particles, position, sf::Blend::Alpha, "blast.png", P_Blast);


There are three classes :
Class Particle      - with all particle properties
Class ParticleSystem -
Code: [Select]




And Class ParticleManager - which call the ParticleSystem object and goes through every system

So when I need a effect what i do is make that effect a sub class of particle system like
Code: [Select]




Then I call the SetParticleInfo() - >Update ->Draw() for each system

The most important i think is the update function...where the particle position, velocity etc needs to be changed.

for example:
Code: [Select]




The update part is where i am having a hard time.... :(

Code: [Select]



 
With this update I am having the particle go only in first and third quadrant ( its right since values are all positive or all -ve)  .

So my doubt are how to randomly give values to the particle so that they can be move in all four quadrants randomly.

Let me know how to improve it...I know the post is very lengthy to read...

All suggestions are welcome... :shock:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Random number generator
« Reply #7 on: June 07, 2011, 03:30:18 pm »
Code: [Select]
ParticleSystem(int num, sf::Vector2f pos, sf::Blend::Mode mode, std::string file, ParticleSystemType type)The std::string can be passed as const-reference (const std::string&), avoiding an unnecessary copy in some cases. For sf::Vector2f, a const-reference doesn't pay since 2 floats are very cheap to copy, and since there is no dereferencing necessary with pass-by-value.

Code: [Select]
std::list<Particle*>Use std::list<Particle> or std::vector<Particle>, there is no need for pointers and manual memory management.

Code: [Select]
delete this;I would never do that, this is almost always bad design. A class normally doesn't know how it has been created and how to destroy its objects in an appropriate way. The one who calls new should call delete (or leave it to some smart pointer).

Code: [Select]
inline void SetNumberOfParticles(int num)inline isn't necessary here. Generally, I would use this keyword rarely. Often, the better choice is to move function definitions to .cpp files, especially because you can change them without recompiling all class clients.

Code: [Select]
float Randomizer1(float min, float max);
int Random(int min, int max);
I would take these functions out of the Blast class, they are not specific to particle systems. Apart from that, you should probably overload them, i.e. use the same names.

Code: [Select]
float a = ((((((8 - 5 + 2) * rand()%11) + 5) - 1 + 1) * rand()%11) + 1) * 0.2;If you already have the random functions, why don't you use them? Your code shouldn't have a single std::rand() call. This allows you to change the implementation of your random number generator, and let the changes affect your system.

Code: [Select]
for( iter = mL_Particle.begin(); iter != mL_Particle.end(); iter++) Although the difference is at most minimal, prefer pre-increment (++iter) because you might save an unnecessary copy.


To create a random velocity, I would create a uniformly distributed angle first.
Code: [Select]
float angle = Random(0.f, 360.f);Then, using std::cos() and std::sin(), you should be able to create a vector from it. You can still randomize the length when multiplying with a random scale.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Random number generator
« Reply #8 on: June 07, 2011, 03:52:32 pm »
Will it be a good logic to have specific shape functions for the particles in each system.

What i mean is ..like for the blast effect , i would try to have a sphere shape with particle position around the sphere.
Then with the updated values all the particle move in a spherical direction.


or should i try something different???

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Random number generator
« Reply #9 on: June 07, 2011, 06:43:57 pm »
Quote
The sf::Randomizer class has been removed from SFML 2, so I wouldn't use it anymore, at the latest if you update to a new SFML version.


Thanks for the heads-up on that, Nex :) Changing my code now.
{much better code}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Random number generator
« Reply #10 on: June 08, 2011, 09:33:04 am »
Quote from: "vicer1234"
or should i try something different???
There are tons of possible designs, I can't tell which one is the best one for your specific use case. But I can tell which design I chose, when I tried to provide a more or less general solution ;)

Let yourself inspire from particle systems in existing libraries, such as SPARK, Irrlicht, Thor, etc...

Quote from: "Jove"
Thanks for the heads-up on that, Nex :) Changing my code now.
Just in case you already use my library: It contains similar functionality as sf::Randomizer, so you don't have to reinvent the wheel.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: