Solved

C Random Numbers 0.0 to 1.0

Posted on 2006-11-04
5
364 Views
Last Modified: 2010-05-18
Hi,
I need to generate random numbers using C in the following range:
0.0 <= X <= 1.0

That will give us the following possible values
0.0
0.1
0.2
...
0.9
1.0

So far, I have been able to generate random numbers, but not to the tenth place.  If anyone knows how to modify my code, or write better code, that is what I need.  Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main() {
srand(time(NULL));

double rnum;
int i;

for(i=0; i<=20; i++) {
    rnum = 1.1 * (float) rand() / (float) RAND_MAX;
    printf("Number: %lf\n", rnum);
}

return 0;
}

0
Comment
Question by:deadite
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
marchent earned 75 total points
ID: 17874402
>>rnum = 1.1 * (float) rand() / (float) RAND_MAX;
rnum = (float)(rand() % 11) /10;

~marchent~
0
 
LVL 8

Author Comment

by:deadite
ID: 17874419
marchent,

That worked perfect, except my printf line still outputs the extra 0's.  Is there a way to get rid of them?  For example:

0.600000
1.000000
0.200000

Thanks
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 17874650
printf("Number: %.1f\n", rnum);
0
 
LVL 8

Author Comment

by:deadite
ID: 17874983
Ozo,
Sorry for the confusion.  I meant get rid of the extra 0's so the number stored is X.X  not X.XXXXXX.  However, your printline shows the results I need, but doesn't store the ones I need.

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 17875050
I'm sorry, I don't understand what you mean when you say the number stored is X.X  not X.XXXXXX
the number is not stored as a string of ASCII digits, the number is stored as a float,
which probably does not even have an exact representation for 0.1
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 125
Turning python script into an applet 12 110
C# Error - Add Failed 12 87
Need help making a program or form for inventory that exports to txt 8 40
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question