Results 1 to 12 of 12

Thread: RANDOM NUMBER

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    how do i do random numbers i got an example but its not what i wanted

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    void main( void )
    {
    int i;

    /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
    srand( (unsigned)time( NULL ) );

    /* Display 10 numbers. */
    for( i = 0; i < 10;i++ )
    printf( " %6d\n", rand() );
    }

    if you test it it will go all over the place and i tried lots of another things to but it just dont work
    WHat would we do with out Microsoft.
    A lot more.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well what did you want?
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    like the forumal to make random numbers in c++ , ranges dosent matter please help
    WHat would we do with out Microsoft.
    A lot more.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I still don't understand what you're after - what's "forumal" meant to be? I am guessing it's a spelling mistake or a typo.

    If ranges don't matter then what's the problem? rand() returns an integer value between 0 and RAND_MAX, which is 32767.

    If you want a number between 0 and 1 or something (I'm guessing what you might want here), then try this:

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <time.h> 
    
    void main( void ) 
    { 
       int i, iRandom; 
       float fRandom0To1;
    
       /* Seed the random-number generator with current time so that 
       * the numbers will be different every time we run. 
       */ 
       srand( (unsigned)time( NULL ) ); 
    
       /* Display 10 numbers. */ 
       for( i = 0; i < 10;i++ ) 
       {  iRandom = rand() % 100;  /* resolution of 0.01 */
          fRandom0To1 = (float)iRandom / 100;  /* float division */
          printf( " %f\n", fRandom0To1 ); 
       }
    }
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    yeah that was a typo ranges do matter sorry
    WHat would we do with out Microsoft.
    A lot more.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    SO no one nows how to make random numbers????? please help some one.... if need somthing that will do it like ramngs in vb thanks.
    WHat would we do with out Microsoft.
    A lot more.

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh was 'forumal' meant to be 'formula'?

    I didn't see this post last time you replied, otherwise I would have responded. rand() returns an integer between 0 and 32767, so assuming you don't need a random integer higher than that, you can just use the mod operator (%) to get a random number in the range you want. For example:

    Code:
    randomNum0To70 = rand() % 71;
    
    randomNum21To40 = 21 + rand() % 20;
    So if you want a general formula:

    Code:
    /* this expression will return a random number between a and b */
    
    a + rand() % (b - a + 1);
    Anyway I think that's what you want. Please, check your messages for typos before you submit your post, it isn't obvious what you mean by 'ramngs' but I'm guess you meant 'ranges'. If you don't make it clear what you need, people won't be able to help you properly. Make sure you give all the information they need. Just saying "this isn't what I want" usually isn't going to get you very far.

    Anyway, hope that helps.
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    sorry i was in a hurry, thanks alot man i was working on that all night . thanks alot..
    WHat would we do with out Microsoft.
    A lot more.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    i have this in my code, this is all i have .

    int nSpace;
    nSpace = 21 + rand() % 20;
    cout << nSpace << endl;
    return 0;

    nothing else is that all i need to have or do i still need the time thing when i do that it comes out to do like 22 and i did it like 30 times and it still comes out to be 22 .
    WHat would we do with out Microsoft.
    A lot more.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 1999
    Posts
    204
    ok i added the srand and it wokred thanks man
    WHat would we do with out Microsoft.
    A lot more.

  11. #11
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Lightbulb

    To get a random number

    int x;
    x=rand();
    printf("%d\n" x);
    Matt

  12. #12
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Umm... yeah that's what the previous 9 posts say.
    Harry.

    "From one thing, know ten thousand things."

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