how to generate a random number

how to generate a random number within the range of 61 to 122
and give it to g

int g = ...

thanks
Last edited on
include <stdlib.h>
int g = rand() % 61 + 122;

right?
Nope.

srand( time(0) );
int g = 61 + rand() % (123 - 61);

I think this should do it.
Nope;

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <appropriate headers>
using namespace std;

int main()
{
    srand(time(NULL);
    int g = rand()%60 + 61;

    cout << g;
    
    cin.get();
    return 0;
}
lol, still wrong :)

rand() % 60 yields [ 0...59 ]
when + 61, the range becomes [ 61 ... 120 ].

Therefore rand() % 62 + 61 should work.

(assuming the range includes both 61 and 122).


EDIT: so Bv202 was right after all.
Last edited on
@jsmith... OH PISS! Somehow i worked out 123 - 61 to be 60... kept thinking it was 121 not 123 :S... haha quite poor considering i do A level maths :|

Edit** which would then make mine the same as Bv202, what was I thinking when I wrote this???
Last edited on
your need to include:
#include <ctime>
Topic archived. No new replies allowed.