Random Number In PowerBuilder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | //////////////////////////////////////////////////////////////////////// // // Function: f_BigRand // // Description: // This function uses 2 calls to Rand to generate Random numbers for large // ranges. It breaks the range into blocks of 32767 (max range for // RAND function) // The limit of the function is 32767 blocks of 32767. // It calls RAND to first determine which block, then calls RAND again to // determine the number in the // selected block. To ensure even probability over the range where // there are multiple blocks, // the same sub range is used for each block. // // Argument al_range // The upper limit of the range of random numbers you want returned. // The lower limit is always 1. The maximum value for the argument is // 1,073,676,289 or 32767*32767. // // Returns // A random number in the range 1 to al_range inclusive. // Or 0 if max range exceeded. // ////////////////////////////////////////////////////////////////////////// // // Revision History // // ----------------------------------------------------------------------- // VERSION CHANGE ID WHO WHEN WHAT/WHY // ----------------------------------------------------------------------- // 6.30 8/4/00 Initial version. ////////////////////////////////////////////////////////////////////////// Long ll_Offset, ll_Return Integer li_Range, li_Blocks // Block size is set to 32767 as this is the maximum range // the RAND function can handle Integer li_BlockSize = 32767 If al_range > 1073676289 Then //Range is too big Return 0 End If //Determine The number of blocks li_Blocks = Truncate(al_range/li_BlockSize,0) //Add an extra block to accommodate any remainder If Mod(al_range, li_BlockSize) > 0 Then li_Blocks = li_Blocks + 1 End If // Improve efficiency for ranges less than Block Size // where there is only one block If al_range < li_BlockSize Then li_Range = al_range Else li_Range = li_BlockSize End If ll_Return = 0 // Loop until the value is in range. // If the value is not in range, calculate the // Offset again to ensure even probability Do Until (ll_Return > 0) And (ll_Return <= al_range) // Calculate a Random Offset using the number // of blocks as the range. // Offsets will range from [0 to (li_Blocks - 1)*BlockSize] // in increments of BlockSize ll_Offset = (Rand(li_Blocks) - 1) * li_BlockSize //Main Calculation ll_Return = ll_Offset + Rand(li_Range) Loop Return ll_Return |
Good Luck! From Source: pbdr.com