Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RaistlinMajere

help with rand()

This topic is 5433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
If you want the random numbers to be different each time you execute your program (i.e. random), you will have to seed rand(). Include ctime, in addition to cstdlib:

#include <cstdlib>
#include <ctime>

...
{
    srand( unsigned ( time(0) ) );

    ...

    // now can use rand()
}
...

You don't really need the explicit type cast in there, though.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 2, 2003 6:47:03 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by joseph drake
num = srand(time(NULL));

I think this is more portable

Don''t listen to this, it is very very wrong.

read this: http://www.robertjacobs.fsnet.co.uk/random.htm

it tells the correct way to use rand, even a way that will distribute the numbers more evenly than if you used %

Share this post


Link to post
Share on other sites
srand(time(NULL));
a = rand() % 50;

I used srand(time(NULL)) as a seed. Anything else I tried to use
under linux failed. Maybe I didn''t try hard enough:-)

Share this post


Link to post
Share on other sites
Does the C++ Standard state any rules for the internal random generator used in the overloaded version of std::random_shuffle that takes only two arguments?

For example, in GCC's STL implementation, it seems to merely use rand() (or lrand48() if it is available), and doesn't seem to seed it. In which case, the other overloaded version, which takes a random number generator functor as a third argument, might be more suitable.

Just to add clarity (and reference), I'm talking about these declarations:

template <typename RandomAccessIter>
void random_shuffle(RandomAccessIter first, RandomAccessIter last);

template <typename RandomAccessIter, typename RandomNumberGenerator>
void random_shuffle(RandomAccessIter first, RandomAccessIter last, RandomNumberGenerator& rand);


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 2, 2003 7:35:20 PM]

Share this post


Link to post
Share on other sites
Hello,
the thing these people are forgeting is that you want only four of the cards 1 through 13. I used a array and random number to fill each one then ran through 13 if then''s to check if there was more than 4 of that card, then the while loop ran out after all 52 spots were assembled. then you just assign a varible to the current block in the array, and when you need a new card you just do a yourvariablehere++; You might want to look into making the computer count cards and bet, i did this and it makes the game alot more interesting.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!