Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate random number from 0.5 to 1.0

Tags:

c

How to Generate the random number from 0.5 to 1.0 .

like image 529
kiran Avatar asked Sep 16 '10 07:09

kiran


People also ask

How do I generate a random number between 0.5 and 1 in Excel?

Generating random decimal numbers between 0 and 1Select a blank cell, and type =RAND() into it, and then drag the fill handle to fill the range you need with the formula.

How do you generate random numbers with decimals?

Generate a random decimal within a specified rangeEnter the formula =RAND()*([UpperLimit]-[LowerLimit])+[LowerLimit]. For example, if you'd like to generate a random decimal between one and 10, you may enter =RAND()*(10-1)+1. Press the "Enter" key.

How do you generate a random number between 0 and 1?

Using the random.uniform() function is perfectly suited to generate a random number between the numbers 0 and 1, as it is utilized to return a random floating-point number between two given numbers specified as the parameters for the function.


2 Answers

You can try:

float RandomBetween(float smallNumber, float bigNumber)
{
    float diff = bigNumber - smallNumber;
    return (((float) rand() / RAND_MAX) * diff) + smallNumber;
}
like image 51
Adrian Fâciu Avatar answered Oct 17 '22 16:10

Adrian Fâciu


You should be able to use something like:

double x = ((double)rand()) / ((double)RAND_MAX) / 2.0 + 0.5;

The division by RAND_MAX gives you a value from 0 to 1, dividing that by two drops the range to 0 to 0.5, then adding 0.5 gives you 0.5 to 1. (inclusive at the low end and exclusive at the top end).

like image 27
paxdiablo Avatar answered Oct 17 '22 16:10

paxdiablo