Thread: questions....so many questions about random numbers....

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

    Thumbs up questions....so many questions about random numbers....

    As you can tell by the title, I ask too many questions about random numbers...and they arn't even very useful!

    Well, I hope this will be the last one about random numbers and here it goes (oh yeah, and by the way, I bet stoned_coder will answer this one. He answers all my questions!! THANKS!!)...

    To display a random number, you do this...


    #include <cstdlib>
    #include <ctime>
    #include <iostream>

    using namespace std;

    int main()
    {
    srand(time(NULL));
    cout <<"A random number from 0 to 99: "<< rand() % 100;

    return 0;
    }


    ...but what i'd like to know is how would you store the random number into an int type variable (or any type of variable, for that matter) such as int nrandom; ??

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by face_master View Post
    As you can tell by the title, I ask too many questions about random numbers...and they arn't even very useful!

    Well, I hope this will be the last one about random numbers and here it goes (oh yeah, and by the way, I bet stoned_coder will answer this one. He answers all my questions!! THANKS!!)...

    To display a random number, you do this...


    #include <cstdlib>
    #include <ctime>
    #include <iostream>

    using namespace std;

    int main()
    {
    srand(time(NULL));
    cout <<"A random number from 0 to 99: "<< rand() % 100;

    return 0;
    }


    ...but what i'd like to know is how would you store the random number into an int type variable (or any type of variable, for that matter) such as int nrandom; ??

    Thanks
    -Chris
    you could try something like this.
    its been very long since i touched c++ so i hope its correct.
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int   random_array[10];
    	int i;
    	
    srand(time(NULL));
    cout <<"A random number from 0 to 99: "<< rand() % 100;
    
    // fill the random array wiht random numbers
    for(i=0;i < 10;i++)
       random_array[i]=rand() % 100;
     // print the random numbers to screen  
    for(i=0;i < 10;i++)
       cout <<"random array number"<<random_array[i]<<"\n";   
    
    return 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Couldn't you find a thread OLDER THAN 2001 to bump?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  2. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM