Results 1 to 7 of 7

Thread: Random

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131

    Random

    Hello, how can I get a number between 1 and 6 in C++?
    Khavoerm Irithyl

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char* argv[])
    {
    	srand((unsigned)time( NULL ));
    	cout<<rand() % 7<<endl;
    	return 0;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Oh, this will generate 0 too. You may use if..else to check if it is 0 use rand again.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by Vlatko
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char* argv[])
    {
    	srand((unsigned)time( NULL ));
    	cout<<(rand() % 6)+1<<endl;
    	return 0;
    }
    How about that?
    I'm a VB6 beginner.

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    LOL

    When you are in a hurry even that happens.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by Vlatko
    LOL

    When you are in a hurry even that happens.
    It happens to me all the time.
    I'm a VB6 beginner.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131
    Thanks for the help
    Khavoerm Irithyl

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