why doesnt it randomize?

This is a discussion on why doesnt it randomize? within the C++ Programming forums, part of the General Programming Boards category; hey im using dev c++ and turbo c++ at school and i made a program at school and tried to ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    28

    Exclamation why doesnt it randomize?

    hey im using dev c++ and turbo c++ at school and i made a program at school and tried to use it at home and it didnt work the error said that the random function didnt work. so i need help. by the way i already made a thread to this but no one answered, i guess its because my subject diddnt seem so interesting to people.

    Here is the portion of my code that contains the random function

    Code:
    clrscr();
    
    randomize();
    while(c!=0)
    {
    cout<<"You have "<<points<<" points"<<endl;
    cout<<lives<<" lives remaining";
    cout<<"Enter your guess NOW!"<<endl;
    cin>>x;
    
    a=random(10);
    //the code doesnt end here so its not like its missing brackets or 
    //anything
    please if u have used dev c++ before and know even the tiniest bit of info that could help me please do.

    Thanks-in advance

  2. #2
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    857
    what's the library you're using?
    Yoshi

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    You might be interested in using rand() and srand() instead, if nothing works.
    Using Dev-C++ beta under Win XP Pro. That or g++ on Mandrake Linux 9.0 .

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Many of the early borland compilers didn't comply to the standards (in borland's defense ANSI-C++ was virtually non-existant). Nonetheless, random() isn't a c standard function. Nor is it covered by any STL class. Do as WarBaboon suggested.

  5. #5
    Shadow12345
    Guest
    call
    srand(GetTickCount())
    once before you can get into any methods, after that all random functions will work

    EDIT:
    You need to include time.h into your project!

    EDIT1:
    Umm will srand(GetTickCount()) even work with devC++?
    Last edited by Shadow12345; 11-15-2002 at 06:21 AM.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    The following should work for Dev C++ (well, it did work for me,heh).

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main() {
        srand(time(NULL)); // seed randomization
        // now generate and display 10 random numbers 0-9
        for(int i = 0; i < 10; i++)
          {
          cout << "random number: " << rand()%10 << endl;
          }
      system("PAUSE");
      return 0;
    }
    Using Dev-C++ beta under Win XP Pro. That or g++ on Mandrake Linux 9.0 .

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    by the way i already made a thread to this but no one answered, i guess its because my subject diddnt seem so interesting to people.
    Maybe people got tired of reading the same question so many times and giving the same answer? Just a thought.

  8. #8
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    Dev acctualy has a random number guessing game in the example things

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Well the way I know is:
    Code:
    #include <ctime>
    #include <cstdlib>
    
    srand(time(0));     //to randomize
    rand();                  //to generate the random number
    but I think it depends on the library you're using.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 02:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 07:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 02:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 12:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 03:31 AM

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