Random Floating point numbers

This is a discussion on Random Floating point numbers within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I am trying to write a function that will gerenerate a random number that is of float value. Now I ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-12-2004
Viruss
 
Posts: n/a
Default Random Floating point numbers

I am trying to write a function that will gerenerate a random number that is
of float value. Now I need it to generate between two values like so:

Lets say I have a value of .5 and 1.5. I want hte number to be between .5
and 1.5. Here is what I got so far:

function GetRandomValue($min, $max)
{
$range = $max-$min;
$num = $min + $range * mt_rand(0, 32767)/32767;

$num = round($num, 4);

return ((float) $num);
}

Now this seems to work to get me a value between like .04 and .03 but
nothing else. Anyone have any other suggestions?


Reply With Quote
  #2 (permalink)  
Old 06-12-2004
Viruss
 
Posts: n/a
Default Re: Random Floating point numbers

"Viruss" <haha@hah.ca> wrote in message
news:RiAyc.734446$oR5.666689@pd7tw3no...
> I am trying to write a function that will gerenerate a random number that

is
> of float value. Now I need it to generate between two values like so:
>
> Lets say I have a value of .5 and 1.5. I want hte number to be between .5
> and 1.5. Here is what I got so far:
>
> function GetRandomValue($min, $max)
> {
> $range = $max-$min;
> $num = $min + $range * mt_rand(0, 32767)/32767;
>
> $num = round($num, 4);
>
> return ((float) $num);
> }
>
> Now this seems to work to get me a value between like .04 and .03 but
> nothing else. Anyone have any other suggestions?
>
>


Never mind that function seems to have taken care of it I was entering bad
data. Thanks though


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:08 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0