Thread: Random numbers (about the tutorial)

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Random numbers (about the tutorial)

    The tutorial displays the following code on how to generate random numbers...
    Code:
    #include <stdlib.h> //for rand
    #include <stdio.h> //for printf
    #include <time.h>   //for time
    
    int main(void)
    {
        srand(time(NULL));
        printf("A random number from 0 to 99: %d", rand() % 100);
        return 0;
    }
    My question is: how do i change this snippet so that it uses the cout << thing instead of the printf thing. If this is not possible, please explain why and tell me if I should continue to use the cout << thing or just go with the printf thing.

    Thanks
    -Chris

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<cstdlib>
    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    {
    srand(time(NULL)); 
    cout<<"A random number from 0 to 99 :- "<<(rand()%100);
    return 0;
    }
    Last edited by Stoned_Coder; 08-26-2001 at 07:15 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Code:
    #include <cstdlib> //for rand 
    #include <ctime> //for time 
    #include <iostream>
    
    using namespace std;
    
    int main() 
    { 
    srand(time(NULL)); 
    cout <<"A random number from 0 to 99: "<< rand() % 100; 
    return 0; 
    } 

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    SC - You beat me to it while I was colouring my code .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Promlems with Random Numbers
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 03-20-2002, 08:46 PM