Printing random decimal numbers in a text file

This is a discussion on Printing random decimal numbers in a text file within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to create a simple program that can generate numbers in a separate .txt file. The random ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Printing random decimal numbers in a text file

    Hi,

    I am trying to create a simple program that can generate numbers in a separate .txt file.

    The random numbers generated should be:
    1. normalised (if i am not mistaken, it should be divided by 32767) to be within the range of -1.000000 to 1.000000
    2. up to 6 decimal places

    However I encountered a couple of problems:
    1. The random numbers I generate are whole numbers (when what I want are random numbers up to 6 decimal places)
    2. When I try to normalise the numbers (i.e. divide it by 32767), all my numbers become 0.00000

    Here is the program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h>
    
    double main() 
    {
      double i;
      FILE *file;     
    
      file = fopen("numbers.txt", "w"); 
    
      if(file==NULL) {
        printf("An error has occurred.\n");
        return 1;
      }
      
    	srand(time(NULL));
    
      for(i=0 ; i<1000 ; i++) /* 1000 random numbers to be generated*/
      {
    
    	double random;
    
      random = (rand()%32767+1)/32767; 
     
        fprintf(file, "%lf\n", random); 
        
      }
    
      fclose(file); 
      return 0;
    }
    Hope someone would be able to help!

    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    2. When I try to normalise the numbers (i.e. divide it by 32767), all my numbers become 0.00000
    Code:
    random = (rand()%32767+1)/32767;
    That calculation uses all integer math (division) until it gets assigned to the double. You want to be using floating point division in there at some point.



    Code:
    double main()
    Huh?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Printing random decimal numbers in a text file

    Yeah. you made mistake in calculation.
    random = (rand()%32767+1)/32767;

    It takes 32767 values integer and this (rand()%32767+1) value as float. so it returns 0.000000

    Change the calculation like this it will work.
    Code:
    random = (rand()%32767+1)/32767.0f;
    Now, it will take both values are float. so it will generate random numbers.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Re: Printing random decimal numbers in a text file

    Thanks it works!

    I have another problem:
    I would like to now print these numbers in an array (with a 'tab' spacing in between the numbers). Any suggestions in editing the code?

    I should look something like (in the text file):
    0.123456 0.234567 0.345678
    0.789456 0.456232 0.756159
    ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. How to copy a word or numbers from text to other location
    By trancedeejay in forum C Programming
    Replies: 12
    Last Post: 02-09-2006, 05:43 AM

Tags for this Thread


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