Random Float between 0 and 1 in PHP
You may use the standard function: lcg_value().
Here's another function given on the rand() docs:
// auxiliary function
// returns random number with flat distribution from 0 to 1
function random_0_1()
{
return (float)rand() / (float)getrandmax();
}
How to generate random numbers between 0.1 and 0.01
You can create an uniform distribution on an arbitrary interval with
((double)rand()/RAND_MAX)*(i_end-i_start)+i_start
where i_start
and i_end
denote the start and end of the interval.In your case try
((double)rand()/RAND_MAX)*0.09+0.01
Converting random byte (0-255) to a float in PHP?
Try the simple linear relationship
$f = $r / 255.0;
where $r is the random byte, and $f is the random float.This way, when $r=255, $f is 1.0 and when $r=127, $f is 0.498
to get 0.5 for r=127 would require a different relationship.
How to create a random float in Objective-C?
Try this:
(float)rand() / RAND_MAX
Or to get one between 0 and 5: float randomNum = ((float)rand() / RAND_MAX) * 5;
Several ways to do the same thing. Generating random floats, summing to 1, with minimum value
IIUC, you want to generate an array of k
values, with minimum value of low=0.055
.
It is easier to generate numbers from 0 that sum up to 1-low*k
, and then to add low
so that the final array sums to 1. Thus, this guarantees both the lower bound and the sum.
Regarding the high
, I am pretty sure it is mathematically impossible to add this constraint as once you fix the lower bound and the sum, there is not enough degrees of freedom to chose an upper bound. The upper bound will be 1-low*(k-1)
(here 0.505).
Also, be aware that, with a minimum value, you necessarily enforce a maximum k
of 1//low
(here 18 values). If you set k
higher, the low bound won't be correct.
# parameters
low = 0.055
k = 10
a = np.random.rand(k)
a = (a/a.sum()*(1-low*k))
weights = a+low
# checking that the sum is 1
assert np.isclose(weights.sum(), 1)
Example output:array([0.13608635, 0.06796974, 0.07444545, 0.1361171 , 0.07217206,
0.09223554, 0.12713463, 0.11012871, 0.1107402 , 0.07297022])
PHP equivalent of javascript Math.random()
You could use a function that returns the value:
PHP
function random() {
return (float)rand()/(float)getrandmax();
}
// Generates
// 0.85552537614063
// 0.23554185613575
// 0.025269325846126
// 0.016418958098086
JavaScript
var random = function () {
return Math.random();
};
// Generates
// 0.6855146484449506
// 0.910828611580655
// 0.46277225855737925
// 0.6367355801630765
@elclanrs solution is easier and doesn't need the cast in return.
Update
There's a good question about the difference between PHP mt_rand()
and rand()
here:
What's the disadvantage of mt_rand?
Related Topics
Datetime Now PHP MySQL (+ Pdo Variant)
Sort Array Based on the Datetime in PHP
Parsing Xml Data Using PHP to Put into MySQL Database
Does HTML_Entity_Decode Replaces &Nbsp; Also? If Not How to Replace It
Get the Get Variables from a Url String
Wordpress Paginate_Links - How to Use It
Docx File Type in PHP Finfo_File Is Application/Zip
Insert Backbone.Js Model into MySQL Database
Capital Letters in Class Name PHP
Usort Issue with Decimal Numbers
What Is the Correct Format for a Blowfish Salt Using PHP's Crypt
Merging Overlapping Ranges in PHP Arrays
Composer:How to Add a Dependency Without Network Connection
How to Display HTML to the Browser Incrementally Over a Long Period of Time
How to Wrap Text Using Imagick in PHP So That It Is Drawn as Multiline Text