Random numbers in C++

Here’s how you create seemly random numbers i C++. This will generate a random number from 1 to 10.


#include <ctime> // For time()

using namespace std;

void main()
{
srand(time(0));  // Initialize random number generator.
r = (rand() % 10) + 1;
}

For you legacy compiler users without namespace-support.

#include "cstdlib";  // Legacy for for srand() and rand()

0 Responses to “Random numbers in C++”



  1. No Comments Yet

Leave a Reply





Robert Sundström

Tweets

  • Waiting for the VS 2010 Beta 2 to be released to public. Only a couple of hours left. But still not sure when. Is it local time? 1 month ago
  • Writing meaningless programs.. 1 month ago
  • I consider myself being a logical being, but my logic is flaud. 1 month ago
  • SvD: Facebook gör dig smart – men Twitter fördummar (http://bit.ly/14XGNr 2 months ago
  • 2 weeks have passed since I came to Karlskrona. I pretty much enjoy living here. College has started and so far little coursework. 2 months ago

Recent Comments

Steve on Windows 3.11 on a virtual…
catchmikey on Wakoopa
Hillary on First days in Delsbo
wilhol on PC or Mac?

Categories

Calendar

September 2009
M T W T F S S
« Aug   Oct »
 123456
78910111213
14151617181920
21222324252627
282930  

Blog Stats

  • 15,577 hits