 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 26th, 2004, 09:34 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
random number
i want to a generate random number between 1 to 20 giving priority to some number.
Such as I want 6 for 50% time
11 for 20% time
(it mean that if i generate 10 times then I will get 6 at least five times, and 11 and least 2 times)
Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net
__________________
Best Regard:
Md. Zakir Hossain (Raju)
www.rajuru.xenexbd.com - my blog with PHP scripts, PHP Book Review and many more
|
|

December 2nd, 2004, 07:56 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
is there anyone to help me, moderator please help me
Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net
|
|

December 2nd, 2004, 08:21 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you want it to do that, then its not very random, is it? :/
Only way to do that kind of thing is with some complex math.
P.S. why you want a moderator to help? Theres plenty of other good coders that aren't mods ;)
Many shoes,
Jamez/SiliconFuRy
|
|

December 2nd, 2004, 01:25 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks for reply,
but can you help me giving the algorithm
none yet help me that's why i wanted moderator help
Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net
|
|

December 6th, 2004, 11:18 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well you're gonna have to devise a new function to do this. You're also gonna think how you're going to want to syntax the function, for instance...
MakeRandom(x,y,z);
where random is selected between x and y, and a 50% bias towards z, so
1,20,6 would pick a rnadom number between 1 and 20 with a 50% bias towards 6.
From this, you could generate an array, and populate it with the values from 1 and 20 (use a loop), but when you populate, make 50% of the values the value z (in our example, 6), so you'd get an array something like....
shoes = Array(1,2,3,4,5,6,7,8,9,10,6,6,6,6,6,6,6,6,6,6);
Then use array_rand(shoes) to pick a value from that.
Bingo :D
Many shoes,
Jamez/SiliconFuRy
|
|

December 6th, 2004, 11:19 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Actually that prob wouldn't work so well, as the numbers from 11-20 would never be picked, but you can see what im basically getting at.
Many shoes,
Jamez/SiliconFuRy
|
|

December 7th, 2004, 08:48 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Actually if you generate your array EACH type (dynamically) you call the function You COULD use that function. You create your array by populating your Biased numbers (i.e. the 50% as 6 and 20% as 11) then you fill the remaining values with a random number from using the srand and rand functions using a range from 1 to 20. Then you AGAIN use the SRAND and RAND function to determine which value from the array you are going to use.
In this way, (Statistically speaking) you will get the BIASED value as you suggest but still have a probablitiy of getting all the other numbers a smaller percentage of the time.
The key is to REPOPULATE the array EACH TIME, as SiliconFury points out, if you don't then you will definitely miss some values.
Hope this helps.
Paul Gardner
------------------
PHP-LIVE help
Via Web @ http://www.mnetweb.co.uk/irc
Via IRC Client pgardner.net:6667
room #PHP
|
|

December 7th, 2004, 10:52 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sounds good, I'd use the srand and rand function to populate the array with 50% of random values and the other 50% of the preferred bias value.
Many shoes,
Jamez/SiliconFuRy
|
|
 |