rand()

This is a discussion on rand() within the C Programming forums, part of the General Programming Boards category; Hello, We try to generate a random number between 0 and 1. Does anyone know how? Thanks!...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    29

    Question rand()

    Hello,

    We try to generate a random number between 0 and 1.
    Does anyone know how?

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes.

    Edit: Salem pointed this out recently in another thread:
    http://www.catb.org/~esr/faqs/smart-...ons.html#prune
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    29
    Thanks Dino, but it doesn't specify how to make the number in the range 0 to 1.

    Matsp, If anyone knows how to generate a random number between 0 and 1 please help us (is this an English forum?)

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Do some math!!

    1) Figure out what RAND_MAX is.
    2) cast the returned value as a double
    3) divide #2 by #1 above
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Livnat View Post
    Matsp, If anyone knows how to generate a random number between 0 and 1 please help us (is this an English forum?)
    So, have a look at the link I posted: Ask smart questions, unless you want smart-ass answers. Yes, I know how to do that.

    So what have you tried so far?
    Do you want an integer value (one or zero) or a floating point in the range 0.0 - 1.0 or 0.0 - 0.99999999... ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    More generally, I suggest that you read Prelude's article on using rand().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    The typical solution for this is to use C and C++'s remainder operator to force the range down to whatever is desired. For example, to shrink the range into [0..N), one might do this:
    Code:
    int r = rand() % N;

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by blurx View Post
    ...to shrink the range into [0..N),...
    My my, proper range notation. I'm impressed!!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by Dino View Post
    My my, proper range notation. I'm impressed!!
    I just copied off that off the link that laserlight gave.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    >I'm impressed!!
    I get "it's" and "its" right most of the time too. How's that for impressive?
    My best code is written with the delete key.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Wow.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 12:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 12:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 09:13 PM

Tags for this Thread


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