parameters for random

i know when you put random there is no parameters but say i want it to pick 6 random numbers between 1 - 100 how would i do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <ctime>

int main(int argc, char* argv[])
{
    srand((unsigned)time(0)); // See link.
    int randNum = 0;

    std::cout << "Six numbers between 1-100:" << std::endl;
    for(int i = 0;i < 6;i++)
    {
        randNum = (rand()%100 + 1); // The max number goes after the modulus operator.
        std::cout << randNum << " ";
    }
    std::cout << std::endl;

    return 0;
}


http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

To generate random numbers in a range, just modify the rand() formula to something like this.

 
randNum = (rand()% ((45+1) - 15) + 15);


Where the range is 15-45.
Last edited on
or simply do this::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void main()

{
	 clrscr();
	 int num,i;
	 randomize();    //randomize initializes the random number generator with a random value
	 for(i=0;i<6;i++)
	 {
	    num=random(100); //random with one parameter ...an int here
	    cout<<num<<endl;
	 }
	getch();
}

@empress: that's not standard C++. There is no iostream.h header in C++, cout and endl are in the std namespace, and conio.h is DOS specific IIRC. Your compiler is very old. This will confuse beginners learning modern C++.
Topic archived. No new replies allowed.