rand() in decimals.

This is a discussion on rand() in decimals. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm wondering how do I generate random numbers in decimals? Meaning between 0-1. Any ideas? rand() returns from the ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    rand() in decimals.

    Hi, I'm wondering how do I generate random numbers in decimals?
    Meaning between 0-1.

    Any ideas? rand() returns from the minimum of 1.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Read Prelude's article on Using rand(), in particular about creating a uniform deviate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Thanks for the link, my mind must've slipped as this could also be done.

    Code:
           float k = (rand()%10);
           k = k/10;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >float k = (rand()%10);
    >k = k/10;
    Apparently you didn't read the entire article. :P
    My best code is written with the delete key.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,620
    I've always done it like so:
    Code:
    float frand(int chop, int perk = 10) {
       return (float(rand() % chop) * pow(10.0, float(perk))) / pow(10.0, float(perk));   }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Yarin View Post
    I've always done it like so:
    Code:
    float frand(int chop, int perk = 10) {
       return (float(rand() % chop) * pow(10.0, float(perk))) / pow(10.0, float(perk));   }
    You probably want to read (and understand) Prelude's page too - that is, if you care about the distribution of your random numbers. [And if you REALLY want good random numbers, don't use rand - as it's by no means guaranteed to be "good"].

    --
    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
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,620
    I did read it quite a while ago, but I figured out that rand() was producing pretty much the same thing as her version of rand(). All I have to do is call srand() every 1k calls to rand().
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Yarin View Post
    I did read it quite a while ago, but I figured out that rand() was producing pretty much the same thing as her version of rand(). All I have to do is call srand() every 1k calls to rand().
    Calling srand again after calling it the first time will NOT improve the distribution of your sequence - it will restart the sequence with a different seed, and depending on the seed value, it may give you the same sequence as last time or a different sequence. But it won't make the distribution any better.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  3. multiple decimals = problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:32 AM
  4. Decimals To Binary
    By kas2002 in forum C++ Programming
    Replies: 8
    Last Post: 06-08-2002, 11:12 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

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