php - Why would rand() return a negative value when min and max values are positive? -


I have a simple piece of PHP code for which to create a random number, however, even though the input is always positive Sometimes a negative product returns

Here's my debug code :.

  $ debt = rand ($ this- & gt; gross domestic product * 0.02, $ this-> GDP * 0.17); Echo "& lt; p & gt; GDP:" $ this-> Gross domestic product "rand = & lt; strong> (" $ this-> gross domestic product * 0.02 "," $ this- & gt; gross domestic product * 0.17 "....),". $ Loan. "& Lt; / strong> "; Here is an example product:  
  Gross domestic product: 219254674605 Rand (4385093492.1, 37273294682.85) = 75,276,999 Gross domestic product: 345,015,694,865 rand (6900313897.3, 58652668127.05 ) = - + 1636353016 Gross domestic product: 90445390920 rand (1808907818.4, 15375716456.4) = -165604705 Gross domestic product: 341284,9650 rand (68,256,993, 58,01,84,440.5) = 347,516,196 Gross domestic product: 293391315 rand (58782226.3 , 499,648,923.55) = 119,181,875 Gross domestic product: 26,369,065 rand (527381.3, 4482741.05) = 3,632,416 gross domestic product: 215,838,135 rand (43,16,76 2.7, 36,692,482.95) = 28,784,811 gross domestic product: 511,763,530 rand (10235270.6, 86999800.1) = 39,954,394 gross domestic product: 42,416,245 rand (848324.9, 7210761.65) = 3,974,882 gross domestic product: 75,090,235 rand (15,01,804.7 , 12,765,339.95) = 5,201,966  

then why can a rand () return a negative return to two positive numbers?

Any help would be greatly appreciated!

Because you are seeing an integer overflow in arguments.

According to its logic to its two int values. 32-bit machine along with 32 bit (for at least PHP) so when you give 2 31 and arguments greater than minus; 1 They start overflow and zero; 2 31 again

Obviously, if you need big values, then you have to code it yourself, while building 64-bit numbers of only two 32-bit numbers works just as it is because your maximum value With only one modulo operation can not be performed, because delivery distribution. How to create an evenly distributed random integer between a 0 and a certain upper limit for a good implementation, you can customize it according to the 64 bit integer accordingly.

While a good choice for random numbers usually uses MT 19937 instead of a poor LCG, here it does not help because its logic is also used as int There are also.


Another option that you may want to consider is if you do not always need every possible value to choose:

  • Generate a random floating-point value between 0 and 1 by calling

      $ rnd = mt_rand () / (double) mt_getrandmax ()  < / Pre> 
  • Determine the limit of numbers you need:

      $ min = $ this-> GDP * 0.02; $ Max = $ it- & gt; GDP * 0.17; $ Limit = $ max - $ min;  
  • Multiply by the already obtained random floating-point value and add the minimum:

      $ value = $ min + $ Range * $ rnd  
  • Now you have a random value between your chosen ranges. It is distributed almost evenly, although there are discrete phases between adjacent random numbers because you are pulling a random number of 32 bits in a large number of bits. If this is not a problem for you, then go ahead.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -