Reply
Halp...random php generator
Old 12-31-2007, 09:14 AM Halp...random php generator
santowhir's Avatar
Experienced Talker

Posts: 30
I'm developing an online game and I need to generate some numbers randomly but they need to be slightly fixed in terms of probability...firstly the user has an already generated number between 1 and 5 (this is not the problem) but that number they are given is then generated to give another number between 1-5 but the chances of this has to be fixed so that the lower numbers often give out a lower number and the higher numbers often give a higher number....hopefully the sums below will make it clearer...

so for example if you have been give a 1 the chances of your next number being a 1 is 25/50

number 1
1 = 25/50
2 = 15/50
3 = 5/50
4 = 4/50
5 = 1/50

number 2
1 = 10/50
2 = 25/50
3 = 10/50
4 = 3/50
5 = 2/50

number 3
1 = 2/50
2 = 10/50
3 = 25/50
4 = 10/50
5 = 3/50

number 4
1 = 1/50
2 = 4/50
3 = 10/50
4 = 25/50
5 = 10/50

number 5
1 = 1/50
2 = 4/50
3 = 5/50
4 = 15/50
5 = 25/50

can anyone help?
__________________
// stottie.net // - xhtml, css, php, mysql, javascript, xml, xsl
santowhir is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 12-31-2007, 02:02 PM Re: Halp...random php generator
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 950
Name: Jeremy Miller
Location: Reno, NV
I usually do something like this:
PHP Code:
<?php
$first_random_number 
rand(1,5);

$random_probability_number rand(1,50); //Since all your denominators are 50.

$second_random_number 0//Initialize variable.

switch($first_random_number) {
  case 
1:
    if (
$random_probability_number == 1) {
      
$second_random_number 5;
    } else if (
$random_probability_number <= 5) { //5 = 4 + 1
      
$second_random_number 4;
    } else if (
$random_probability_number <= 10) { //10 = 5 + 4 + 1
      
$second_random_number 3;
    } else if (
$random_probability_number <= 25) { //25 = 15 + 5 + 4 + 1
      
$second_random_number 2;
    } else {
      
$second_random_number 1;
    }
    break;
  case 
2:
    
//I'll let you finish
    
break;
}

echo 
$second_random_number;
?>
I'm not giving you all of the code as you didn't indicate if this was for a homework assignment and you didn't provide a try yourself.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 12-31-2007, 02:28 PM Re: Halp...random php generator
NullPointer's Avatar
Will Code for Food

Posts: 473
Name: Matt
Location: Irvine, CA
What you need is a variant of a random number generator with gaussian distribution.

I modified this function from something I found on php.net:

PHP Code:
<?php

function gauss()
{   
    
$x=random_0_1();
    
$y=random_0_1();
    
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
    
    return 
$u;
}

function 
gauss_ms($mean=0.0,$standardDev=1.0)
{   
    return 
round(abs(gauss()*$standardDev+$mean));
}
//generates a random number between 0 and 1 with psudo-even distribution
function random_0_1()
{   
    return (float)
rand()/(float)getrandmax();
}

?>
What you'll want to do is call gauss_ms with the mean parameter as the number you want your random number to be closest to, you'll also need to use the modulus operator because there is a chance that the number generated can be higher than your max.

So if you want to generate a number close to 2 with a max of 5 do the following:

PHP Code:
<?
$x 
guass_ms(2)%5;
?>
I didn't really test this code but it should work, hope it helps.

Also the solution Jeremy provided may work better but it requires a bit more code.
__________________
http://tinsology.com/ - Under construction

Last edited by NullPointer : 12-31-2007 at 02:33 PM.
NullPointer is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Halp...random php generator
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13745 seconds with 13 queries