Thread: How to correctly implement random decimals?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    20

    How to correctly implement random decimals?

    I've been experimenting with this method for the last five minutes and it hasn't properly produced what exactly I want. I want a program that outputs 10 random decimal numbers. All of the decimal numbers should be lower than 2.00, meaning, that some of the numbers that actually get placed on screen(output) should be like:

    1.91
    0.19
    0.45
    etc.

    Well, enough talking. Let me paste what I've done. I hope the next person that post can come up with a quick, peaceful(not bashful) solution.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
     double number;
    
     for(int i=0; i<=10; i++)
    {
     number= (rand() % 1) + ( (rand() % 99) * 0.10);
     cout << number << endl;
    }
    
     cin.get();
     return 0;
    }
    Last edited by ZooTrigger1191; 02-16-2003 at 10:56 PM.

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>decimal integers

    an integer is a whole number, can't be decimal, thats a double or float.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    Some advice to future responders. If you have some minor to say just PM me. The whole point of me posting this topic is to get this problem resolve. Not to better my english.

    Note: I was in a rush when I was typing that. Notice how I edit my post like 2 times already.

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    isnt it

    (rand () % (high - low + 1) ) + low;
    so if you use decimal...never tried..sorry cant compile right now i just reformat hard drive.....going to reinstall visual studio 6.0 ent in a few mins
    nextus, the samurai warrior

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Some advice to future responders. If you have some minor to say just PM me. The whole point of me posting this topic is to get this problem resolve. Not to better my english.
    note to authors...don't wanna hear it, don't post it.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Any number modulus one is zero. If you want 0 or 1 you need to modulus by 2. Also, if you modulus 99, then 99 will not be a possible answer. And last, if you multiply by .1 then your range is from .1 to 10 instead of .01 to 1. I think what you need is:

    number= (rand() % 2) + ( (rand() % 100) * 0.01);

    OR

    number=(rand()%200)*.01;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  3. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 02:16 PM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. Random Decimals
    By TJJ in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2004, 03:46 PM