Solved

C Random Numbers 0.0 to 1.0

Posted on 2006-11-04
5
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

623 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