4

I need to generate a 4 digit random number in C++

I used the following code

#include<time.h>

int number;

number=rand()%1000;
srand ( time(NULL) );

but its doesn't gives a total random number

Sudantha
  • 15,684
  • 43
  • 105
  • 161
  • 1
    As a complement to the accepted answer, there is a very nice article concerning the proper usage of rand() and srand() at http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx. – manneorama Nov 01 '10 at 07:55

3 Answers3

24
number = rand() % 9000 + 1000;

There are 9000 four-digit numbers, right? Starting from 1000 till 9999. rand() will return a random number from 0 to RAND_MAX. rand() % 9000 will be from 0 to 8999 and rand() % 9000 + 1000; will be from 1000 to 9999 . In general when you want a random number from a to b inclusive the formula is

rand() % (b - a + 1) + a

Also note that srand() should be called only once and before any rand().

If you do consider the numbers between 0 an 999 inclusive to be "four digit numbers", simply use rand() % 10000 in that case. I don't consider them to be but I'm covering all bases, just in case.

HTH

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I'm pretty sure the OP can figure out the first part himself. And by the way the fact that 0045 is not a 4 digits number is arguable. The important point is to call `srand` first in order to get a "total random number". – log0 Nov 01 '10 at 07:18
  • 1
    +1. And might I also suggest that, if there's even the _slightest_ possibility that this program will run multiple times in a tight loop, insert a `sleep(1)` before `srand(time(0))` so that the seeds will be better selected. This will avoid the inevitable "why aren't my random sequences random?" questions :-) – paxdiablo Nov 01 '10 at 07:25
4

Remember to call srand only once.

Take a look at Boost.Random, if you don't like the distribution of rand.

// produces randomness out of thin air
boost::mt19937 rng;

// distribution that maps to 1000..9999
boost::uniform_int<> fourdigits(1000,9999);

// glues randomness with mapping
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(rng, fourdigits);

// simulate rolling a die
int x = die();
dalle
  • 18,057
  • 5
  • 57
  • 81
  • 3
    Killing a sparrow with a nuke, are we ? :) Kidding, I like boost, but it just might be too much for the op who seems to be a beginner – Armen Tsirunyan Nov 01 '10 at 07:07
  • 2
    Though to be fair, it's standard in C++0x, so should be a standard way of distributing numbers. – GManNickG Nov 01 '10 at 07:12
  • @GMan: Still whether or not it is standard it is just too complicated for a beginner. My point is it's great we will have it in the standard but maybe it should be kept for intermediate/advanced learners – Armen Tsirunyan Nov 01 '10 at 07:14
  • 1
    @Armen Tsirunyan: I agree with the nuke thing. But although using Boost.Random require a lot more code, I can see that people find `boost::uniform_int<> fourdigits(1000, 9999)` more logical than `rand() % 9000 + 1000`. – dalle Nov 01 '10 at 09:23
2

You must seed your random generator srand before calling rand()

#include<time.h>

srand( time(NULL) );
int number = rand() % 10000; // number between 0 and 9999
log0
  • 10,489
  • 4
  • 28
  • 62