random numbers between 1500 and 3000

This is a discussion on random numbers between 1500 and 3000 within the C Programming forums, part of the General Programming Boards category; I want to generate random numbers between 1500 and 3000. Is there a way to do this using either of ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    random numbers between 1500 and 3000

    I want to generate random numbers between 1500 and 3000. Is there a way to do this using either of the following random number generating code examples?

    Much appreciated.

    Code:
    srand( (unsigned)time( NULL ) );
    printf( "  %d\n", rand() );
    Code:
    random_number = (int) (3000.0*rand()/(RAND_MAX));

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Just use the same code you would use for generating a rndom number between 0 and 1500, and then add 1500 to it.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Great thinking.

    Thankyou very much.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21