Solved

C Random Numbers 0.0 to 1.0

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 94
mapAB Challlenge 35 128
Program to display an alert on Windows Toolbar 2 71
silent install of security banner via msiexec command 4 76
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 …
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now