Solved

C Random Numbers 0.0 to 1.0

Posted on 2006-11-04
5
362 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now