Advertisement Jump to content
Sign in to follow this  
Sophia Bush

Random in C++

This topic is 4903 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

Hello everyone, Im new in the forum, I Hope I will enjoy here. Im currently learning C++, And I want to know, The function random need to include any libary like iostream? *Sorry for my English, Im from Israel (But I was born in New York =))

Share this post


Link to post
Share on other sites
Advertisement
Rand always outputs the same numbers though. If you are working on a decent project, you might as well use boost::rand. Googled. The first link on the page should be it.

Share this post


Link to post
Share on other sites
rand() from cstdlib/stdlib.h only outputs the same numbers if you don't seed it with a different value for each run with srand(). For that matter the pseduo random number generators in boost::random will also produce the same numbers each time if you don't seed them properly too. Don't blame a library just because you don't know how to use it.

Share this post


Link to post
Share on other sites
You can also

#include <time.h>

then in a function

int iRandom;
srand(time(0));
iRandom = rand()%7+1;

It will roll a number between 1 and 7.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
You can also

#include <time.h>

then in a function

int iRandom;
srand(time(0));
iRandom = rand()%7+1;

It will roll a number between 1 and 7.


Yup!, but I'll add that you only need to seed the random numbers once (srand only at startup)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!