You are here: Re: Need help to custom the rand function , « PHP Programming Language « IT news, forums, messages
Re: Need help to custom the rand function ,

Posted by The Natural Philosopher on 02/01/08 16:16

C. (http://symcbean.blogspot.com/) wrote:
> On 31 Jan, 19:29, The Natural Philosopher <a...@b.c> wrote:
>> Fred wrote:
>>> Hi guys,
>>> I was wondering how could i get a random number exemple between 1 and
>>> 100 but with n% of getting some value over a number x.
>>> Thanks a lot in advance.
>>> Fred
>> Never mind Jerry, No one else does.
>>
>> So you want a random number from a weighted distribution?
>>
>> Your spec doesn't contain quite enough data to formulate the formula,
>> since there are an infinite number of distributions that have N% over
>> value X..
>>
>> One example, is that you first of all get a random number between 0 and
>> 100: If this is greater than N, then select a random number greater than
>> X, else select one less than X.
>>
>> So in pseudo code where RAND(X) returns a number between 0 and X-1...
>>
>> if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(100-X));
>
> This is not a god way to get a weighted distribution (and the OP is
> NOT asking for a weighted distribution)

(a) I never said it was.
(b) Agreed. But some weighting is inherent in what he said he wanted.
(c) it is one way to get what he said he wanted though.

The problam is he wnats SOME form of weighted distribution, ..as evinced
by the actual spec. But there isn't enough to say WHAT weighting he wants.

- you should just generate one
> floating point number and apply a transformation function to it.
> Continuous transformations are quite difficult but since for a
> possible range of 100 integer values a discontinuous transformation
> would be adequate - something like:
>
> function transform($in)
> {
> if (($in<0) || ($in>100)) trigger_error('out of range');
> $mul=array(
> 1=>10, 5=>9, 10=>7, 15=>6....40=>1, 50=>0.9, 55=>0.8...
> );
> return($in * $mul[5*(integer)($in/20)]);
> }
>
> Note the values above will emphasize the mean rather than what the OP
> was asking for, but could be adapted for purpose by tweaking the $mul
> values.
>

Indeed.

> C.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация