产生一个更大的随机数的方法
2006-03-27 21:02
246 查看
//////////////////////////////////////////////////////////////////////////
// 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.
//
// Author: David Dennis
//////////////////////////////////////////////////////////////////////////
//
// 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
// 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.
//
// Author: David Dennis
//////////////////////////////////////////////////////////////////////////
//
// 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
相关文章推荐
- dos下使用汇编产生一个随机数方法
- 如何产生不重复的随机数?最容易想到的方法,是逐个产生这些随机数,每产生一个,都跟前面的随机数比较,如果重复,就重新产生。这是个很笨的方法,且比较次数呈线性增长,越往后次数越多。其实这些比较是多余的,
- MATLAB产生一个设定比例0和1的随机数数组的方法
- Python使用当前时间、随机数产生一个唯一数字的方法
- 控制台产生一个随机数的方法
- 设计一个类,该类中有一个方法,该方法使用Random类随机产生10个三位数字(如636)的随机数,并把产生的10个随机数存入数组中。然后在另一个类中输出这10个数
- 程序员面试金典——解题总结: 9.17中等难题 17.11给定rand5(),实现一个方法rand7()。也就是,给定一个产生0到4(含)随机数的方法,编写一个产生0到6(含)随机数的方法
- java中产生随机数的方法
- sort方法的使用、随机数的产生
- 一个产生随机数的类
- PHP产生不重复随机数的5个方法总结
- 产生一个int数组,随机向数组中插入1-100的随机数且不能重复
- C++随机数的产生方法
- 用rand()和srand()产生为随机数的方法总结
- OC产生随机数的几种方法
- C/C++如何产生随机数方法整理
- java中产生随机数的几种方法
- 用rand()和srand()产生为随机数的方法总结
- Java中产生随机数的两个方法
- 关于随机数不得不说的一个方法