Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Random but more likely closer to a fixed point
Old 01-06-2010, 08:17 PM Random but more likely closer to a fixed point
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
Ok this may sound strange at first. Basically I wanted to make a random number but closer to a specified point, the code at the moment (cut the rest out) is:

Code:
$tac = 17 // starting number
while($i <= '20'){
$tac = $tac + rand(-1,1);
$i++;
}
So this is basically making the number closer to 17 but can still vary. The reason behind this is a user specifies an average stat for a player so it keeps near that but they aren't all the same.

My code DOES work just hugely increases the execution time as it has to do it 20 times for about 10 stats, for about 30 players, for about 20 teams, for about 4 leagues... meaning it has to do that rand() appox. 480,000 times... which as you can see is quite major!
Stephen. is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-06-2010, 09:01 PM Re: Random but more likely closer to a fixed point
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Why not just add one random number to your starting number?
PHP Code:
$tac 17 rand(-1010); 
They still have the same amount of "randomness". Plus, if php's random number generator is good, all numbers will be picked the same number of times (statistically). So in your code with rand(-1,1) there would be just as many -1 and 0 as 1 (again, statistically). And if we add all those numbers together they should (statistically) equal 0. So $tac will still be 17, or at least very close.

Of course in practice it may vary more, but it is i.e. very unlikely that you'd get $tac to, lets say 5 or 30.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-07-2010, 05:21 AM Re: Random but more likely closer to a fixed point
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
Sorry you must have misunderstood me. I'm starting at say 17. Then it will take 1 or add 1 20times... therefore making the MIN -3 and MAX 37. But because it's between -1 and 1 then overall it's likely that the number in the end is closer to 17. And it's highly unlikely that it does get 37 and -1. So it's still going to be close to 17 but not always as close. Understand?

What you explained is what I'm trying to achieve

Last edited by Stephen.; 01-07-2010 at 05:23 AM..
Stephen. is offline
Reply With Quote
View Public Profile
 
Old 01-07-2010, 07:18 AM Re: Random but more likely closer to a fixed point
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Aha, now I see
Then what you're looking for is probably a random number generator with a "normal distribution" (you can check this out on the web, basically means that numbers will tend to be closer to a given number).

(Dont know all these words in english but I'll try to explain :P)
A normal distribution (also called gauss distribution) is written as N(0,1), where 0 is the mean ("avarge") outcome and 1 is the variance (how much the numbers can differ from the avarage). In your case, with your given example, you'll want N(17,20).

I found a piece of code on php.net which does this:
PHP Code:
<?php

function gauss()
{   
// N(0,1)
    // returns random number with normal distribution:
    //   mean=0
    //   std dev=1
    
    // auxilary vars
    
$x=random_0_1();
    
$y=random_0_1();
    
    
// two independent variables with normal distribution N(0,1)
    
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
    
$v=sqrt(-2*log($x))*sin(2*pi()*$y);
    
    
// i will return only one, couse only one needed
    
return $u;
}

function 
gauss_ms($m=0.0,$s=1.0)
{   
// N(m,s)
    // returns random number with normal distribution:
    //   mean=m
    //   std dev=s
    
    
return gauss()*$s+$m;
}

function 
random_0_1()
{   
// auxiliary function
    // returns random number with flat distribution from 0 to 1
    
return (float)rand()/(float)getrandmax();
}

?>
EDIT: The code I found is a few years old, it is better to use the new functions mt_rand() and mt_getrandmax() instead of rand() and getrandmax().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 01-07-2010 at 07:20 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-07-2010, 09:14 AM Re: Random but more likely closer to a fixed point
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
The way I'm doing it is probability faster I'll look into it though, also why use mt_rand() I thought that was purely for larger numbers?

Just as I said I have a very large script which I'm trying to optimise a little
__________________
Did I help you? If so add to my Talkupation! =]
Stephen. is offline
Reply With Quote
View Public Profile
 
Old 01-07-2010, 11:33 AM Re: Random but more likely closer to a fixed point
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
By using the script I pasted you'll only be calling the rand function two times, where as you call it 20 times. It may look like more but you can strip it down to this:

PHP Code:
<?php
function gauss_ms($m=0.0,$s=1.0)
{
   
$x = (float)mt_rand()/(float)mt_getrandmax();
   
$y = (float)mt_rand()/(float)mt_getrandmax();
   
$u sqrt(-2*log($x))*cos(2*pi()*$y);
   return 
$u*$s+$m;
}
?>
I dont know more than what I've read on php.net, which is that the mt_rand() function is based on another algorithm which gives better randomness and is four times as fast as rand().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-07-2010, 12:18 PM Re: Random but more likely closer to a fixed point
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
Ah excellent, thanks for that. I've added to your Talkupation as you taught me something new :P

EDIT: I did actually read about mt_rand() a couple of years back, not from php.net though. And it only really mentioned that it's more random and it can use bigger numbers too. The fact that random is a fictional word in the sense that computer nor man can say / display a random number still amuses me
__________________
Did I help you? If so add to my Talkupation! =]

Last edited by Stephen.; 01-07-2010 at 12:20 PM..
Stephen. is offline
Reply With Quote
View Public Profile
 
Old 01-07-2010, 12:44 PM Re: Random but more likely closer to a fixed point
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Much obliged
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Random but more likely closer to a fixed point
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 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.27691 seconds with 12 queries