random number function

Thanks
Last edited on
Um...make a function that does the rand() code?
look up the random number class that is already included in C++ you don't need to re-invent the wheel, you do however need to know how to click in the search box and type 'random'
the first 2 entries will be what you need to use. rand() and srand(), and probably both together.

http://cplusplus.com/query/search.cgi?q=random

^^
Last edited on
now that you have read that and are still saying wtf???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <iostream>
#include <cstdlib>
#include <time>
using namespace std;

int randomNum ( int numberOutOf )
{
    return ( 1 + rand() % numberOutOf );
}

int main()
{
    srand(time (NULL));      // so rand() seeds the time, using seconds.

    int num1, num2, num3;

    num1 = randomNum(500);      // random number between 1 and 500
    num2 = randomNum(200);      // random number between 1 and 200
    num3 = randomNum(1000);    // random number between 1 and 1000

    cout <<  "Number 1 is: " << num1 << endl;
    cout <<  "Number 2 is: " << num2 << endl;
    cout <<  "Number 3 is: " << num3 << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.