DragonPrime - LoGD Resource Community
Welcome Guest
  • Good morning, Guest.
    Please log in, or register.
  • October 20, 2017, 09:13:03 AM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: rand() vs e_rand()  (Read 2888 times)
0 Members and 1 Guest are viewing this topic.
Voratus
Guest
« on: May 13, 2004, 02:43:35 PM »

I see what the e_rand function does, but my question is why?

Am I missing something, because it looks like they essentially give you the same thing, a random number between min and max.
« Last Edit: May 13, 2004, 02:44:06 PM by Voratus » Logged
Spider
Guest
« Reply #1 on: May 13, 2004, 03:28:59 PM »

Code:
function e_rand($min=false,$max=false){
   if ($min===false) return mt_rand();
   $min*=1000;
   if ($max===false) return round(mt_rand($min)/1000,0);
   $max*=1000;
   if ($min==$max) return round($min/1000,0);
   if ($min==0 && $max==0) return 0; //do NOT as me why this line can be executed, it makes no sense, but it *does* get executed.
   if ($min<$max){
      return round(@mt_rand($min,$max)/1000,0);
   }else if($min>$max){
      return round(@mt_rand($max,$min)/1000,0);
   }
}

there are two main advantages.  Firstly it only generates a random number if it has to, which saves on processing a random number when the min and max is equal anyway.

Secondly it multiplies by 1000 first, which supposedly gives a more random number, especially when the range is small.

In reality it does do exactly the same thing, although I would suggest using mt_rand() instead of rand() if you don't use the e_rand() function as it is "more random" and much faster.
Logged
Voratus
Guest
« Reply #2 on: May 14, 2004, 08:40:09 AM »

Taking the *1000 /1000 thing, I figured out probably the real difference.
Because of rounding, you will have an equal chance of getting any value in the middle of the range, but half the chance of getting one on the outside of the range.

For example, e_rand(1,10):

You have a 5% chance of getting a 1 (1000-1499 = 500 unique values)
You have a 5% chance of getting a 10 (9500-10000 = 500 unique values)
You have a 10% chance of getting any value in the middle (using 2: 1500-2499 = 1000 unique values)

So I guess thats the only difference.
Logged
Artte
Guest
« Reply #3 on: May 14, 2004, 09:36:01 AM »

True. That actually makes it more fair, we all know how fair any default random number function in any programming language is... (not very if you the same seed, always had to use the system time to fair it up but not very well)
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Welcome to DragonPrime - The LoGD Resource Community!

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
32 Guests, 2 Users
Pocket1975, Helga
Home Forums News Downloads Login Register Advanced Search