Thread: rand() function

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    rand() function

    Hi, this is my first post...great site...i want to ask sth...i made a small program but i have a problem. when we call rand() we get a number betwen 0 and RAND_MAX. I want to have numbers betwwen 0 and 1. Does anyone know how can i do this? The problem is:
    #...
    #...
    main(){
    int x;
    srand(5);
    x = rand();
    x = rand()/RAND_MAX; //output is always 0...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read Prelude's article on using rand(). Look out for the term "uniform deviate".
    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

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    1) Do us all a favor and use the code tags.
    2) Read about integer division and reflect on your code afterwards.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    This is how I generate a random number. It usually works for me.
    Code:
    #include <time.h>
    
    main()
    {
         int x;
         srand(time(NULL));
         x = rand() % 2;
    }
    This sets "x" to a random number between zero and one.
    Last edited by Babkockdood; 04-30-2010 at 04:34 PM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    your problem is simple....

    if you want to get a number between 0 and 1, then you can't declare it as int x;
    you should declare it as float x;

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    rand() function

    thx v33k....it is working...i can now have numbers bettween 0 and 1...but could you make a program with rand + srand where i can have results like these: (after the . i want only one digit)
    e.g.:
    output:
    0.5
    0.6
    0.1
    0
    0
    0.8
    0.9
    0.5
    0.6
    0.7
    0.8
    0.4
    0.6
    i wish you understood what i want...i am working C about 9 months but i miss some simple things..so if sb could help me i would appreciate this...

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    rand() returns an integer between 0 and RAND_MAX. You can use the modulo operator to get a number between 0 and an upper limit, as has already been shown.

    0.1 * 10 is an integer, so is 0.9 * 10.

    In other words, why not just get an integer between 0 and 10 and then divide it by 10?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Floats/doubles are actually bad for 0.1, 0.2, 0.3, because a tenth of a unit is not a "power of two number". If you want fixed precision here, you are better to use an int between 0 and 10, like DeadPlanet says, then when you want to output it as a decimal:
    Code:
    int x;
    printf("%.1f",(float)(x/10));
    This should be fine, because printf by default rounds correctly (to nearest, not down). The reason it may have to round is that one tenth is not exactly represented in memory, as just mentioned. For an illustration of that, run this:
    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    The output is not what you are expecting! However, if I used the %.1f template, the numbers would be correct (it will round the imprecision in the representation out).
    Last edited by MK27; 05-01-2010 at 08:49 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User sandeep080's Avatar
    Join Date
    Apr 2010
    Posts
    17

    Cool This is easy

    use this:
    Code:
    float result;
    
    result=((float)rand())/RAND_MAX;
    now result would contain a random number between 0 and 1

    here is a sample program which prints 10 random numbers between 0 and 1:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
    float result,i;
    for(i=0;i<10;i++)
    {
    result=((float)rand())/RAND_MAX;
    printf("%f\n",result);
    }
    }
    the logic is rand() returns value between 0 and RAND_MAX.. so when we divide by RAND_MAX it'll always give value between 0 and 1(including 0 and 1)

    Last edited by sandeep080; 05-03-2010 at 06:08 AM. Reason: edited the type conversion thanks to EVOEx

  10. #10
    Registered User sandeep080's Avatar
    Join Date
    Apr 2010
    Posts
    17
    ur problem is that ur getting 0 because u have declared x as an integer.. change it to float because the result which u get is a fraction

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    @sandeep080:
    First of all, read the rest of the topic. Your post is a bit... late. But it's even wrong, as rand() returns an integer and RAND_MAX is an integer, meaning that you will divide two integers, and the result will be an integer, not a float.

Popular pages Recent additions subscribe to a feed