Random Problem Generator

This is a discussion on Random Problem Generator within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> int main() { int a; a==rand(5); cout << a << "\n"; system("PAUSE"); ...

  1. #1
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32

    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?

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    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.
    the best things in life are simple.

  3. #3
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    compiler error.

    "implicit declaration of function 'int randomize(...)"

  4. #4
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    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.

  5. #5
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Thanks, now different nums come up, but why are they all in the same range? (example when %500, it is 426, 438, 416, etc.)

  6. #6
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Ohhhh, function with the system clock. How do I make it function absolutely at random?

  7. #7
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    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.

  8. #8
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    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;
    }

  9. #9
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    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
    the best things in life are simple.

  10. #10
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Doesn't work either. "name lookup of 'i' changed for new ANSI scoping"

    "Using obsolete binding at 'i'"

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. A question about C++ random generator and unsigned method
    By joenching in forum C++ Programming
    Replies: 13
    Last Post: 03-14-2005, 04:05 PM
  3. Random Number Generator?
    By DZeek in forum C++ Programming
    Replies: 21
    Last Post: 03-13-2005, 02:11 PM
  4. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  5. Random things are fine, just one problem with them.
    By DarkSFK in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2002, 09:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21