Results 1 to 5 of 5

Thread: random number generation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163

    random number generation

    how to generate random number between 0 to 100 in vc++?
    Purushottam

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    #include <stdlib.h> //for rand func
    #include <time.h> //time func used in seeding

    int main()
    {
    srand(time(NULL)); //seeds the number
    int mynum = rand()%100+1; //gets the random num
    cout<<mynum<<endl; //outputs it

    return 0;
    }


  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you're going to post C++, you need to have:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
        srand(time(NULL)); //seeds the number
        int mynum = rand() % 100 + 1; //gets the random num
        cout << mynum << endl; //outputs it
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And this creates a random number from 1 to 100 (both inclusive), if you want to have from 0 to 100 (both inclusive) you need
    rand() % 101
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    Thanks very much
    Purushottam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width