Help with making a random number

This is a discussion on Help with making a random number within the C++ Programming forums, part of the General Programming Boards category; I made a death clock and my random number generator always makes 41 so i dont know if i did ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    Help with making a random number

    I made a death clock and my random number generator always makes 41 so i dont know if i did something wrong or what someone please help

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    post some code

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    #include <iostream>
    #include <cstring>
    #include <cstdlib>

    using namespace std;

    string answer;

    int main()

    {
    int number>=0 && number<=100);

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    oops it was

    int number=rand();

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    but i always get 41

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Use code tags!

    You must seed the random number generator
    IE
    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {
    	srand(time(NULL));
    	cout << rand()%50 << endl;
    	cout << rand()%50;
    }

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Im still new to this. ok now it doesnt reckonize my inout number so how do connect the random number generator to the number

  8. #8
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    exactly how you did it before

    Code:
    int number = rand();
    Seeding the generator is a one time thing, usually placed at the top of int main

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    ok thanks much

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    ok now numbers are appearing at the beginning of the program and my number still comes up 41

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using namespace std;
    
    string answer;
    
    int main()
    
    {
        int number = rand();
        
        srand(time(NULL));
    	cout << rand()%50 << endl;
    	cout << rand()%50;
        
        // User Information Storage
        char firstname[15];
        char middlename[15];
        char lastname[15];
        char fullname[45];
        
        //Introduction to Deathclock
        cout<<"Razz-ma-tazz : Hello, I am Razz-ma-tazz.\n";
        cout<<"Razz-ma-tazz : First off what is your first name, Darlin?\n";
        cout<<"User : ";
        cin.getline ( firstname, 15 );
        cout<<"Razz-ma-tazz : Ok "<< firstname <<", Now what is your last name?\n";
        cout<< firstname <<" : ";
        cin.getline ( lastname, 15 );
        cout<<"Razz-ma-tazz : Now, if I can just get your middle name.\n";
        cout<< firstname <<" : ";
        cin.getline ( middlename, 15 );
        cout<<"Razz-ma-tazz : "<< firstname <<" "<< middlename <<" "<< lastname <<" am I right?\n";
        cout<< firstname <<" : ";
        cin>> answer;
        
        strcat ( fullname, firstname );
        strcat ( fullname, " " );
        strcat ( fullname, middlename );
        strcat ( fullname, " " );
        strcat ( fullname, lastname );
        
        if (answer == "yes") {
        cout<<"Razz-ma-tazz : Ok baby, we're almost done.\n";
        }
        else if (answer == "no") {
        cout<<"Razz-ma-tazz : Well, It don't madder now baby, it's what we usin.\n";
        }
        cin.get();
        cout<<"Razz-ma-tazz : Alright baby here we go!\n";
        cout<<"Razz-ma-tazz : Kashmahammaflipflap!\n";
        cout<<"Razz-ma-tazz : Your are going to die in "<< number <<" years.\n";
        cout<<"Razz-ma-tazz : Wow, "<< number <<".\n";
        
        if (number <= 24) {
        cout<<"Razz-ma-tazz : Ooh that's too bad, but aaa why don't you let me get the rest of that cash in there.\n";
        }
        else if (number >= 25 && number <= 50) {
        cout<<"Razz-ma-tazz : You still have a while to live your dreams out hunny.\n";
        }
        else if (number >= 51) {
        cout<<"Razz-ma-tazz : Congradulations baby, your going to live a long healthy life!\n";
        cout<<"Razz-ma-tazz : Now, wither its happy or not I dont't dare say...\n";
        }
        cout<<"Razz-ma-tazz : Well, there you go sweetheart. That'll be 35 dollars.\n";
        cout<<"Razz-ma-tazz : Bubye now and may you live a wonderful life.\n";
        cin.get();
    }

  12. #12
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    check this

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	srand(time(NULL));
        int number = rand();
    
        // User Information Storage
        char firstname[15];
        char middlename[15];
        char lastname[15];
        char fullname[45];
    	string answer;
        
        //Introduction to Deathclock
        cout<<"Razz-ma-tazz : Hello, I am Razz-ma-tazz.\n";
        cout<<"Razz-ma-tazz : First off what is your first name, Darlin?\n";
        cout<<"User : ";
        cin.getline ( firstname, 15 );
        cout<<"Razz-ma-tazz : Ok "<< firstname <<", Now what is your last name?\n";
        cout<< firstname <<" : ";
        cin.getline ( lastname, 15 );
        cout<<"Razz-ma-tazz : Now, if I can just get your middle name.\n";
        cout<< firstname <<" : ";
        cin.getline ( middlename, 15 );
        cout<<"Razz-ma-tazz : "<< firstname <<" "<< middlename <<" "<< lastname <<" am I right?\n";
        cout<< firstname <<" : ";
        cin>> answer;
        
        strcat ( fullname, firstname );
        strcat ( fullname, " " );
        strcat ( fullname, middlename );
        strcat ( fullname, " " );
        strcat ( fullname, lastname );
        
        if (answer == "yes") {
        cout<<"Razz-ma-tazz : Ok baby, we're almost done.\n";
        }
        else if (answer == "no") {
        cout<<"Razz-ma-tazz : Well, It don't madder now baby, it's what we usin.\n";
        }
        cin.get();
        cout<<"Razz-ma-tazz : Alright baby here we go!\n";
        cout<<"Razz-ma-tazz : Kashmahammaflipflap!\n";
        cout<<"Razz-ma-tazz : Your are going to die in "<< number <<" years.\n";
        cout<<"Razz-ma-tazz : Wow, "<< number <<".\n";
        
        if (number <= 24) {
        cout<<"Razz-ma-tazz : Ooh that's too bad, but aaa why don't you let me get the rest of that cash in there.\n";
        }
        else if (number >= 25 && number <= 50) {
        cout<<"Razz-ma-tazz : You still have a while to live your dreams out hunny.\n";
        }
        else if (number >= 51) {
        cout<<"Razz-ma-tazz : Congradulations baby, your going to live a long healthy life!\n";
        cout<<"Razz-ma-tazz : Now, wither its happy or not I dont't dare say...\n";
        }
        cout<<"Razz-ma-tazz : Well, there you go sweetheart. That'll be 35 dollars.\n";
        cout<<"Razz-ma-tazz : Bubye now and may you live a wonderful life.\n";
        cin.get();
    }
    What compiler/IDE are you using?

    -edit-
    Note that I moved the string from being global (outside main) to be inside. Global variables should be avoided when having it global provides absolutely no benefits or could be easily made non-global

    Ignore that if you don't understand

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    bloodshed Dev-c++

    the one recommended for beginngers by this site

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    now how would i limit it under like 100 or so?

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Note that I moved the string from being global (outside main) to be inside. Global variables should be avoided when having it global provides absolutely no benefits or could be easily made non-global

    Ignore that if you don't understand
    yeah i understood it

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help regarding random number
    By Bargi in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 02:16 PM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 01:33 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 05:30 AM

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