### random number generator

Can someone help me with my c++ homework:Make a c++ code that prints 1 random number from 5 to 20.

I tried stuff like:
 123456789101112131415161718192021 #include "stdafx.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { srand(time(NULL)); for(int i=1; i<31; i++) { cout<

and

 12345678910111213141516171819202122 #include #include #include /* function main begins program execution */ int main(void) { int i; srand(time(NULL)); for (i = 1; i <= 1; i++) { /* pick a random number from 1 to 6 and output it */ printf("%10d", 1 + (rand() % 2)); /* if counter is divisible by 5, begin a new line of output */ if (i % 5 == 0) { printf("\n"); } /* end if */ } /* end for */ return 0; /* indicates successful termination */ } /* end main */

but none of them work.
Last edited on
if you want to store a random number from 5 to 20

int temp;
temp = rand()%21;
while(temp == 0 || temp == 1 || temp == 2 || temp == 3 || temp == 4)
{
temp = rand()%21;
}
cout<<temp; // the number will be from 5 to 20

Yea it prints me "20"
Last edited on
it will always give you result from 5 to 20
I need a code that changes this number
 12345678910111213141516171819202122232425262728293031 #include #include #include unsigned getRandom( unsigned min, unsigned max ) { unsigned range = max-min+1 ; return rand() % range + min ; } int main() { std::srand(std::time(0)) ; unsigned nRandomNums ; unsigned minimum ; unsigned maximum ; std::cout << "Number of random numbers to generate: " ; std::cin >> nRandomNums ; std::cout << "Minimum: " ; std::cin >> minimum ; std::cout << "Maximum: " ; std::cin >> maximum ; for ( unsigned i=0; i
Cool but now I have a new problem:
I shouldn't use std::
I deleted some parts of the code and now it looks like this:

 123456789101112131415161718192021222324 #include #include #include long long getRandom( long long min, long long max ) { long long range = max-min+1 ; return rand() % range + min ; } int main() { std::srand(std::time(0)) ; long long nRandomNums ; long long minimum=5 ; long long maximum=20 ; std::cout << "count of random numbers: " ; std::cin >> nRandomNums ; for ( long long i=0; i

when I try it without it the only problems are at
 12 std::cout << "Maximum: " ; std::cin >> maximum ;

and

std::cout << '\t' << getRandom(minimum, maximum) << '\n' ;
where I have two errors:
 ...|20|error: 'cout' was not declared in this scope|

and

 ...|21|error: 'cin' was not declared in this scope|

Can someone tell me how to fix this error?
Topic archived. No new replies allowed.