473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help to custom the rand function ,

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
Jan 31 '08 #1
10 2069
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
If you want n% chance of getting a value over x, then it isn't a random
number.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 31 '08 #2
On 31 jan, 14:05, Jerry Stuckle <jstuck...@attglobal.netwrote:
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

If you want n% chance of getting a value over x, then it isn't a random
number.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Wow man that help me a lot =)

Seriously can you point me somewhere that could help ?

Thanks
Jan 31 '08 #3
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));

Jan 31 '08 #4
On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrote:
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));
Thanks for the info, i wont mind on him then =)

Almost there with the code,

Let me add some real number in the exemple :

i know before the random what number i must have over let take 50 for
the exemple,
i also need the random to still pick between 0 and 100, but with a
chance of 60% (exemple) to draw something over 50 .

Thanks
Jan 31 '08 #5
Fred wrote:
On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrote:
>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));

Thanks for the info, i wont mind on him then =)

Almost there with the code,

Let me add some real number in the exemple :

i know before the random what number i must have over let take 50 for
the exemple,
i also need the random to still pick between 0 and 100, but with a
chance of 60% (exemple) to draw something over 50 .

Thanks
Actually, the troll "Natural Philosopher" is one one who should be
ignored. He seldom has anything to say that is correct. I was just
pointing out you really aren't looking for a random number, from your
description. Maybe a minor point to you, but very important when
creating an algorithm.

The real question here is, what kind of distribution do you want? For
example, do you want a linear distribution, an exponential or
logarithmic distribution, or like you said here, a 60% chance of being
over 50?

In the case you cited, you want 1-50 to occur 40% of the time, and
51-100 to occur 60% of the time (approximately). Something like this
might work (not tested but the algorithm should work):

function notReallyRand() (
$temp = mt_rand(0,999);
if ($temp < 400)
return intval($temp/8);
else
return intval((($temp-400) / 12) + 50);
}

It gets a random number between 0 and 999. If the random number is
between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).

If the value is >= 400, it subtracts 400 from the value (0-599) and
divides by 12 (0-49.xx). It add 50 to this and returns the int value
(50-99).

You can adjust values as necessary.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 1 '08 #6
On Jan 31, 8:56 pm, Fred <himse...@gmail.comwrote:
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
Here is the code ;)

for($i=1;$i<=100;$i++){
$rnum=rand(1,100);
if($rnum>x)
$bigx++;
else $smallx++;
$count++;
}
$ratio=$bigx/$count;
echo "Ratio of the created random numbers bigger than x is :" .
$ratio;

Feb 1 '08 #7
On 31 Jan, 19:29, The Natural Philosopher <a...@b.cwrote:
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) - 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.

C.
Feb 1 '08 #8
C. (http://symcbean.blogspot.com/) wrote:
On 31 Jan, 19:29, The Natural Philosopher <a...@b.cwrote:
>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.
Feb 1 '08 #9
On 31 jan, 21:39, Jerry Stuckle <jstuck...@attglobal.netwrote:
Fred wrote:
On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrote:
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));
Thanks for the info, i wont mind on him then =)
Almost there with the code,
Let me add some real number in the exemple :
i know before the random what number i must have over let take 50 for
the exemple,
i also need the random to still pick between 0 and 100, but with a
chance of 60% (exemple) to draw something over 50 .
Thanks

Actually, the troll "Natural Philosopher" is one one who should be
ignored. He seldom has anything to say that is correct. I was just
pointing out you really aren't looking for a random number, from your
description. Maybe a minor point to you, but very important when
creating an algorithm.

The real question here is, what kind of distribution do you want? For
example, do you want a linear distribution, an exponential or
logarithmic distribution, or like you said here, a 60% chance of being
over 50?

In the case you cited, you want 1-50 to occur 40% of the time, and
51-100 to occur 60% of the time (approximately). Something like this
might work (not tested but the algorithm should work):

function notReallyRand() (
$temp = mt_rand(0,999);
if ($temp < 400)
return intval($temp/8);
else
return intval((($temp-400) / 12) + 50);

}

It gets a random number between 0 and 999. If the random number is
between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).

If the value is >= 400, it subtracts 400 from the value (0-599) and
divides by 12 (0-49.xx). It add 50 to this and returns the int value
(50-99).

You can adjust values as necessary.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thats exaclty the kind of algorythm i needed thanks a lot man , you
should have said that first =)
Ive tweak it a bit and its doiing a great job !

Thanks again !

Fred
Feb 1 '08 #10
Fred wrote:
On 31 jan, 21:39, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Fred wrote:
>>On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrote:
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));
Thanks for the info, i wont mind on him then =)
Almost there with the code,
Let me add some real number in the exemple :
i know before the random what number i must have over let take 50 for
the exemple,
i also need the random to still pick between 0 and 100, but with a
chance of 60% (exemple) to draw something over 50 .
Thanks
Actually, the troll "Natural Philosopher" is one one who should be
ignored. He seldom has anything to say that is correct. I was just
pointing out you really aren't looking for a random number, from your
description. Maybe a minor point to you, but very important when
creating an algorithm.

The real question here is, what kind of distribution do you want? For
example, do you want a linear distribution, an exponential or
logarithmic distribution, or like you said here, a 60% chance of being
over 50?

In the case you cited, you want 1-50 to occur 40% of the time, and
51-100 to occur 60% of the time (approximately). Something like this
might work (not tested but the algorithm should work):

function notReallyRand() (
$temp = mt_rand(0,999);
if ($temp < 400)
return intval($temp/8);
else
return intval((($temp-400) / 12) + 50);

}

It gets a random number between 0 and 999. If the random number is
between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).

If the value is >= 400, it subtracts 400 from the value (0-599) and
divides by 12 (0-49.xx). It add 50 to this and returns the int value
(50-99).

You can adjust values as necessary.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thats exaclty the kind of algorythm i needed thanks a lot man , you
should have said that first =)
Ive tweak it a bit and its doiing a great job !

Thanks again !

Fred
I would have - had you been clear about what you wanted!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 1 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
0
by: Skybuck Flying | last post by:
I found some rand source code on this link: http://www.codeguru.com/forum/showthread.php?t=312416&goto=nextnewest void __cdecl srand (unsigned int seed) { #ifdef _MT _getptd()->_holdrand =...
5
by: vib | last post by:
Hi there, i) Is there any other way to replace the rand()of standard C lib? ii) How efficient is the rand()? Thanks in advance. vib
4
by: Bill Burris | last post by:
Hi, With VS .NET 2003 the rand() function sometimes returns a number equal to RAND_MAX. The docs say: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Does this...
2
by: I. Myself | last post by:
And it has to run on Windows, so it can't use xplt. I would prefer that it use the simplest multi-dimensional model, z = k + a*x1 + b*x2 + c*x3 + d*x4 Anyone have such a thing? Thanks, ...
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
13
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.