in the faq

This is a discussion on in the faq within the C++ Programming forums, part of the General Programming Boards category; i was just chekin out some faq stuff and i found a randomizer that could help me but it seams ...

  1. #1
    Jhanoosh
    Guest

    in the faq

    i was just chekin out some faq stuff and i found a randomizer that could help me but it seams to generate an error even when i run it alone
    Code:
    #include <stdlib.h>
    
    int rand(void);
    
    
    
    #include <stdlib.h> //for rand
    
    #include <stdio.h>  //for printf
    
    
    
    int main(void)
    
    {
    
        randomize();
    
        printf("A random number from 0 to 99: %d", rand() % 100);
    
        return 0;
    
    }
    it says that randomize() is an undeclared identifier
    maby im overlooking somthing small, which could very well be as i am new to c++

    thanks

  2. #2
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Check this code for randomizing....

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       int i;
    
       printf("Ten random numbers from 0 to 99\n\n");
       for(i=0; i<10; i++)
          printf("%d\n", rand() % 100);
       return 0;
    }

    This code will always... give the same result of 10 random numbers...
    You can use srand() function with rand() function to get always different random numbers...by putting
    srand( time ( NULL ) );
    the line before... using rand() function.
    Now you will have to include.. time.h for time() function..
    Hope it will help...
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  3. #3
    jhanoosh
    Guest
    ok im trying to understand this... so if i wanted a function i maby could do this
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <iostream.h>
    
    
    int myrand();
    int main(void)
    {
       
    
       
       cout << endl;
       cout << myrand();
          
    
       return 0;
    }
    
    void myrand()
    {
    	  srand( time ( NULL ) ); 
    	  printf("%d\n", rand() % 100););
    	return 0;
    
    
    }
    hmm i try this but my compiler screams at me
    im just trying to simplify is so i can just get a random # by calling one function name that i can use throughout my program

  4. #4
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Originally posted by jhanoosh

    int myrand();
    int main(void)
    {

    cout << endl;
    cout << myrand();


    return 0;
    }

    void myrand()
    {
    srand( time ( NULL ) );
    printf("%d\n", rand() % 100);
    return 0;


    }
    Your complier must shout ... because.. you declared a function myrand() with return type int and in the defination header you are using void return type of your function... and you are also returning 0 with statement.. return 0; ...
    I have modified your program.. check out ... the code.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <iostream.h>
    
    int myrand();
    int main(void)
     {
    
       cout << endl;
       cout << myrand();
    
       return 0;
     }
    
     int myrand()
       {
         srand( time ( NULL ) );
         return ( rand() % 100 );
       }
    Hope this will solve all your problem....!
    Last edited by jawwadalam; 11-16-2002 at 08:30 PM.
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM
  3. FAQ - it is not a true list of FAQ
    By alpha561 in forum C Programming
    Replies: 1
    Last Post: 05-25-2002, 06:40 PM

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