Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts

    random number generator, c++

    Can anyone see why this always shows the random number as 99?

    PHP Code:
    #include <iostream>
    #include <windows.h>

    int main(){
        
    int x 1;
        
    int y 99;
        
    std::cout << "A random number between " << << " and " << << " is: " << rand()%x+y;
        return 
    0;


  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    You need to seed it:

    srand(time(NULL))

    Be sure to include time.h

  • #3
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try rand()%(x+y). It will work better!

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Thanks Not only did the (x+y) work better, but it was the only way it would work


  •  

    Posting Permissions

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