random number generation

Apr 18, 2013 at 7:25am
hey guys,
my question is quick and simple. how do i generate random numbers but between 1 and 52?
i know how to generate numer between 0 and 52 but no idea how to do it if it starting with 1. thanks a lot :)
Apr 18, 2013 at 7:30am
Have you tried Googling?
Apr 18, 2013 at 7:33am
http://www.cplusplus.com/reference/random/

Alternatively, [0..51]+1.
Apr 18, 2013 at 7:59am
Well i can do that but my professor told us in class not to use google becz it is not always a credible source. Anyways thank you guys :)
Apr 18, 2013 at 8:02am
Try this

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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using std::cout;
using std::endl;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;


int main(){

int randomNumber;

//randomize random number generator using current time
srand(time(0));

cout<<"Generate random numbers between 1-52"<<'\n'<<endl;

for(int counter=1;counter<=10;counter++){
        randomNumber=1+rand()%52;
        
        cout<<randomNumber<<' ';
        if(counter==5){
                cout<<endl;
        }//end if
        
}//end for
cout<<endl;

//type your favorite pause statement HERE e.j "cin.ignore();" or whatever you want

return 0; //indicate successful termination
}//end main




/*
*
* from C++ How to program,5th edition book:
*
* randomNumber=shiftingValue + rand() % scalingFactor;
*
* where shiftingValue is equal to the first number in the desired range of consecutive integers and scalingFactor is equal to the width of the desired range of consecutive int egers. The exercises show that it is possible to choose integers at random from sets of values other than ranges of consecutive integers.
*/
Topic archived. No new replies allowed.