Why Doesn't This Work??!!

This is a discussion on Why Doesn't This Work??!! within the C Programming forums, part of the General Programming Boards category; thanks to Michael Ashley... Code: // An example showing how to generate random floating // point numbers between 0 and ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Angry Why Doesn't This Work??!!

    thanks to Michael Ashley...
    Code:
     
    // An example showing how to generate random floating
    // point numbers between 0 and 1.
    
    // Michael Ashley / UNSW / 02-Apr-2003
    
    #include <sys/time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    int main(void) {
        struct timeval t;
    	// Note that the structure timeval is defined in <sys/time.h> as {int t.tv_sec int t.tv_usec}
    	// Here t.tv_sec is the time in seconds and 
    	// t.tv_usec is the fractional part of the time in microseconds
    
        int i;
    
        // Obtain the time of day, to microsecond resolution.
    
        assert(0 == gettimeofday(&t, NULL)); 
    
        printf("The time is %ld.%06ld seconds since the Unix epoch\n",
    	   t.tv_sec, t.tv_usec);
    
        // Use the number of microseconds as the seed for the system
        // random number generator.
    
        srandom(t.tv_usec);
      
        // Generate and print ten random integers between 0 and RAND_MAX.
        
        for (i = 0; i < 10; i++) {
            printf("%ld\n", random());
        }
    
        // Generate and print ten random numbers between 0 and 1.0.
        
        for (i = 0; i < 10; i++) {
            printf("%.15f\n", random()/((double) RAND_MAX));
        }
    
        return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I proclaim this post, vague post of the week.

    Anyway, it doesn't work because whatever half-assed version of <sys/time.h> you have doesn't have the functions used in this program. Most of them are not required by ISO sys/time.h as I understand it. That's fine because you can very easily fix this. If you include <time.h>, assuming <sys/time.h> doesn't already include it, you can replace all the functions with more standard ones.
    Sent from my iPadŽ

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    for (i = 0; i < 10; i++) {
            printf("%.15f\n", random()/((double) RAND_MAX));
        }
    should be
    Code:
    for (i = 0; i < 10; i++) {
            printf("%f\n", ((double)rand() / ((double)(RAND_MAX)+(double)(1)) ));
        }
    a sample code which gnerated randonm number from [0,1] and [0,1000] floating point numbers
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main()
    {
        int i;
        double  X;
        
        srand(time(0));
        for(i=0;i<10;i++)
        {
            
            X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
            printf("%f \t %f\n",X,X*1000);
            
        }
        getchar();
        return 0;
    }
    
    /*my ouput
    0.009277         9.277344
    0.624451         624.450684
    0.529999         529.998779
    0.419037         419.036865
    0.858154         858.154297
    0.952148         952.148438
    0.746246         746.246338
    0.705444         705.444336
    0.593781         593.780518
    0.759460         759.460449
    */
    ssharish2005

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by ssharish2005

    a sample code which gnerated randonm number from [0,1] and [0,1000] floating point numbers

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main()
    {
        int i;
        double  X;
        
        srand(time(0));
        for(i=0;i<10;i++)
        {
            
            X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
            printf("%f \t %f\n",X,X*1000);
            
        }
        getchar();
        return 0;
    }
    
    /*my ouput
    0.009277         9.277344
    0.624451         624.450684
    0.529999         529.998779
    0.419037         419.036865
    0.858154         858.154297
    0.952148         952.148438
    0.746246         746.246338
    0.705444         705.444336
    0.593781         593.780518
    0.759460         759.460449
    */
    ssharish2005

    Uh i see,

    *but what should i do to confine the generated numbers to a range of say [0, 0] to [(Pi/2), 1]?

    *Also, what should i change to produce 1000 random numbers instead of 10?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >*Also, what should i change to produce 1000 random numbers instead of 10?
    to get 1000 change the for loop conition

    >*but what should i do to confine the generated numbers to a range of say [0, 0] to [(Pi/2), 1]?
    u got to mul the randon generated number (from the rand i.e X) by PI/2 instead ....

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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