Hi am having a problem trying to generate random numbers in my program and I need some help.
Here is my situation. I made a class and each object of the class got a function called getrandom () that returns a random number,
I then made a vector of three objects and used a loop to examine the numbers generated per object , to my amazement all three objects generate the same number and that number is only random between different program runs
Is there anyway that each of my three objects could generate indipendent
Random number within the same loop, below is my code for clarity.
int getrandom() {srand (time(NULL)); return( rand()%100);}time() result changes relatively rarely whn compared to the speed of code execution. That means that in your loop 10 calls to time() will probably yield same result. So you wil reseed rng with same seed making next value the same for every call. You should not call srand more than once in your program. Move call to it to the beginning of the main().
@lowestone and @minnippa well you people are awesome haha,
When I moved srand to main it works just fine as I wanted , that is all I
Wanted but is there another way of generating random numbers if there is
Mybe I can weigh between them and srand for the most effective one.
I'll be very glad to if you can give me a clue.
srand()/rand() are usually uses pretty crappy RNG. Also rand() % something fails at providing uniform distribution.
C++11 provides a whole library for random number handling, <random>.It provides a bunch of tweakable generators (However you should use only std::mt19937 unless you have a good reason to use something else). And many distributions. Honestly it has everything you need. http://en.cppreference.com/w/cpp/numeric/random
Like:
Well this is amaizing , I knew there was another way out coz I dont trust srand so much
Haha, mean while I'll be looking into this random yeah. But I know one day i'll be
Better as you @minnippa haha. Thank you for the help.
@anup30 I guess this std::vector<int>vec{3}; /// should vec(3) is not wrong
New standard ( uniform intialization) and gives you even extra allowance for inclass
Container intialization . Generally I prefer it to vec(3) c++ 11. Plus I don't get where the
There is a problem with uniform initialization: if it is possible to threat parameters as initializer list, it would do so.
As vector do have constructor which takes initializer_list<T>, Init list for int would clash with constructor which takes int or two ints.
So vector<int> x{3} creates a vector with single element with value 3. vector<int*> y{3} creates a vector with 3 null elements.
#$# getting confused now where should I use uniform intialization only on user defined types without a constructor that would conflict with inti list? Now I dont know which.
I can see the point @anup30 was making then.
Just remember this. Soon you will remember what STL classes has to offer you and will notice such traps.
Using braces initialisation or uniform initialization is a matter of preference, but there are some places where only one of them can be used. Pick one and fall back to other when needed. I personally prefer braces.
Well now understand those aren't constructors.@minnippa what should
I do to learn the best of c++ and avoid all this flaws and misconceptions
Or should i just stick to what am learning till the real thing reveal itself
Through experience . How did you learn all this youself?? . Advice will be greatly appreciated.
Bumped into many common problems, listened programmers who knew better, read many books on programming, spent many hours rummaging through C++ standard trying to find explanation for specific behavior, googled a lot...
Thank you for advice , I know soon i'll be good enough too forget about the intimidating
Bugs I come across day after another. Thenks i'll read hard , i am sure you people had such challeges
While learning.