# Random Problem Generator

• 03-31-2002
marcusbankuti
Random Problem Generator
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main()
{
int a;
a==rand(5);
cout << a << "\n";
system("PAUSE");
return 0;
}

I am in the first steps of creating a math question generator. Why is the outcome here always 575? Shouldn't it be random between 0 and 5?
• 03-31-2002
xlnk
Code:

```#include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> int main() {     int a;      randomize();     a=rand(5);     cout << a << "\n";     system("PAUSE");     return 0; }```
and for a==rand(5), it should be a=rand(5), your not testing for equality, your assigning.
• 03-31-2002
marcusbankuti
compiler error.

"implicit declaration of function 'int randomize(...)"
• 03-31-2002
itld
seeding
howdy,
try srand() to seed the rand() function with the system clock--

#include <time.h>

srand(time(0));
int a = rand()%5;

M.R.
• 03-31-2002
marcusbankuti
Thanks, now different nums come up, but why are they all in the same range? (example when %500, it is 426, 438, 416, etc.)
• 03-31-2002
marcusbankuti
Ohhhh, function with the system clock. How do I make it function absolutely at random?
• 03-31-2002
itld
totaly random
howdy,
try this it will create 20 totaly random nuimbers between 0 and 500.

Code:

```#include <stdlib> #include <iostream> #include <time.h> using namespace std; int main(int argc, char* argv[]) { int a; srand(time(NULL)); for(int i = 0 ;i <20; i++){ a = (rand()%500); cout <<a<<"\n"; } system ("pause");         return 0; }```

M.R.
• 04-01-2002
marcusbankuti
What's wrong with this code? I can't find a parse error (according to my compiler the parse error is before if)

#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#include <string.h>
#include <conio.h>
using namespace std;

int main(int argc, char* argv[])
{
int a, b;
cout << "How many random numbers do you want? ";
cin >> b;
cout << "\n";
srand(time(NULL));
for(int i = 0 ;i <b; i++){

a = (rand()%500);
cout <<a<<"\n";
}
system("pause");
cout << "\n";
}
if(i==20)
{
system("pause");
}
return 0;
}
• 04-01-2002
xlnk
Code:

``` #include <stdlib.h> #include <iostream.h> #include <time.h> #include <string.h> #include <conio.h> using namespace std; int main(int argc, char* argv[]) { int a, b; cout << "How many random numbers do you want? "; cin >> b; cout << "\n"; srand(time(NULL)); for(int i = 0 ;i <b; i++) { a = (rand()%500); cout <<a<<"\n"; } system("pause"); cout << "\n"; if(i==20) { system("pause"); } return 0; }```
you had an extra '}' in front of if
• 04-01-2002
marcusbankuti
Doesn't work either. "name lookup of 'i' changed for new ANSI scoping"

"Using obsolete binding at 'i'"
• 04-01-2002
Shiro
You used variable i out of scope. I think it only exists in the for-loop so can't use it anymore after the for-loop has ended. Try to make it a function-local variable (don't know if that is the correct English term), just like variables a and b.