Thread: Random generator 0 1

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    Random generator 0 1

    Hi

    I'd like to write a random number generator between 0 and1.
    I'm doing like that:

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
    int b;
    float r;
    
    srand(time(NULL));
    
    printf("%d",RAND_MAX);
    
    for (b=11;b>0;b--){
    
            r=rand();
    
            printf("%f\n", r/RAND_MAX);
    
            }
    return 0;
    }
    but i get this output:

    Code:
    21474836470.216477
    0.471353
    0.312465
    0.644094
    0.796645
    0.112260
    0.999378
    0.374815
    0.463826
    0.021919
    0.175329
    why the first one is not normalized?

    Thanks

    D.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You printed RAND_MAX without a newline, so you confused that and the very first pseudorandom number printed. Try:
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
        int b;
        float r;
    
        srand(time(NULL));
    
        printf("%d\n", RAND_MAX);
    
        for (b=11;b>0;b--){
            r = rand();
    
            printf("%f\n", r/RAND_MAX);
        }
        return 0;
    }
    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
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    many thanks, I made an easy error.

    D.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, the range you get is [0.0, 1.0], but [0.0, 1.0) is more commonly used, methinks. If you want the latter, you would have to say, add 1.0 to RAND_MAX in the divisor.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

Tags for this Thread