Results 1 to 9 of 9

Thread: Random number

  1. #1
    ChimpFace9000
    Guest

    Post

    Does windows have a random number function?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Don't think so, but the standard library does:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    void main() {
        cout << rand() << endl;
    }
    rand returns a number between 0 and RAND_MAX.
    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

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    or this

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
       srand(time(NULL));
         cout<<1*rand()%6<<endl;
    return 0;
    }

  4. #4
    Guest
    How do i get one between 0 and 99?

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    on mine you would do 0*rand()%99

    parksies I think you would do this


    #include <iostream>
    #include <cstdlib>

    using namespace std;

    void main() {
    RAND_MAX = 99
    cout << rand() << endl;
    }

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    RAND_MAX is already defined -- use:
    Code:
    float myrand(float lower, float higher) {
        return (((float)rand() / (float)RAND_MAX) * (higher-lower)) + lower);
    }
    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

  7. #7
    Guest
    Thanks, but both those return the same sequence of numbers each time.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Seed the random number using srand at the start of your program, using the time as stated earlier.
    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

  9. #9
    Guest
    It STILL gives the same sequence of numbers.

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