Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random in C++

Started by Sophia Bush Aug 20, 2005 at 8:03 PM 4 replies 800+ views
Original Post
Sophia Bush
Sophia Bush
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 =))
Shai
Shai
linkie
"It's better to regret something you've done than to regret something you haven't done."
orcfan32
orcfan32
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.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
SiCrane
SiCrane
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.
kontrolkl
kontrolkl
You can also

#include

then in a function

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

It will roll a number between 1 and 7.
SiliconMunky
SiliconMunky
Quote:
Original post by deathwearer
You can also

#include

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)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.