random number generator
Feb 16, 2013 at 10:47am UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
for (int i=1; i<31; i++)
{
cout<<rand() % 60 <<" " ;
if (i % 10 == 0)
cout<<endl;
}
system("pause" );
return 0;
}
and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/* 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 Feb 16, 2013 at 11:02am UTC
Feb 16, 2013 at 11:02am UTC
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
Feb 16, 2013 at 11:04am UTC
Yea it prints me "20"
Last edited on Feb 16, 2013 at 11:31am UTC
Feb 16, 2013 at 11:06am UTC
it will always give you result from 5 to 20
Feb 16, 2013 at 11:32am UTC
I need a code that changes this number
Feb 16, 2013 at 11:53am UTC
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
#include <cstdlib>
#include <ctime>
#include <iostream>
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<nRandomNums; ++i )
std::cout << '\t' << getRandom(minimum, maximum) << '\n' ;
}
Feb 16, 2013 at 7:40pm UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <cstdlib>
#include <ctime>
#include <iostream>
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<nRandomNums; ++i )
std::cout << getRandom(minimum, maximum) << "\n" ;
}
when I try it without it the only problems are at
1 2
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.