Hi everyone,

i need to generate random numbers between two values a & b, where both a and b less than 1.

Example

a random number between a = 0.60 and b = 0.69.

what i am trying to do is in C program

temp_ins = a + (drand48())*(b - a + 0.01);

Is the above correct way to generate the random number between values between a and b that are less than 1 but greater than 0.

## All 3 Replies

I would multiply both your example numbers(0.60 and 0.69) by 100 and go from there.

You might find some help here..

#include <time.h>
#include <stdlib.h>

int main ()
{

srand (time(0));
temp_ins = ((rand()%10)+ 60)/100

}

this should get you a new random number between 60-69 every time. Then the number is divided by 100 for example: 68 becomes .68

commented: Entirely wrong. Integer division here always results in 0 -1

#include <time.h>
#include <stdlib.h>

int main ()
{

srand (time(0));
temp_ins = ((rand()%10)+ 60)/100

}

this should get you a new random number between 60-69 every time. Then the number is divided by 100 for example: 68 becomes .68

thanks everyone for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.