产生一个更大的随机数的方法

//
// 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
### 回答1: 好的,以下是一个用 JavaScript 写的产生随机数的程序示例: ```javascript // 产生随机数的函数 function generateRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } // 产生随机数的范围 const minNum = 1; const maxNum = 100; // 产生随机数 const randomNumber = generateRandomNumber(minNum, maxNum); // 输出结果 console.log(`产生随机数是:${randomNumber}`); ``` 这个程序通过 Math.random() 函数产生一个 0 到 1 之间的随机数,然后根据需要的范围将其转换成所需的随机数,最后将结果输出到控制台中。你可以根据需要修改 minNum 和 maxNum 变量的值来改变随机数的范围。 ### 回答2: 使用JavaScript编写一个生成随机数的程序非常简单。你可以使用内置的Math对象中的random()方法生成一个介于0到1之间的随机数。 以下是一个示例代码: ```javascript // 生成一个介于0到1之间的随机数 var randomNum = Math.random(); console.log(randomNum); // 生成一个介于0到10之间的随机数 var randomNumInRange = Math.random() * 10; console.log(randomNumInRange); // 生成一个介于1到100之间的随机整数 var randomInt = Math.floor(Math.random() * 100) + 1; console.log(randomInt); ``` 这段代码中,使用Math.random()方法生成一个0到1的随机小数。如果你需要生成一个指定范围内的随机数,可以将返回的随机小数乘以所需范围的大小。如果你要生成一个随机整数,可以结合Math.floor()方法将随机小数向下取整,并加上偏移量来限定整数的范围。 注意,由于Math.random()方法生成的是一个随机数,所以生成随机数并不是真正的随机数。如果需要更复杂的随机数生成算法,可以考虑使用其他库或自行编写算法。 ### 回答3: 下面是一个用JavaScript编写的随机数生成程序: ```javascript // 生成一个0到1之间的随机数 var randomNum = Math.random(); console.log(randomNum); // 生成一个指定范围内的随机整数 function getRandomInt(min, max) { min = Math.ceil(min); // 向上取整 max = Math.floor(max); // 向下取整 return Math.floor(Math.random() * (max - min + 1) + min); } var randomInt = getRandomInt(1, 10); console.log(randomInt); ``` 这个程序有两个示例。第一个示例是生成一个0到1之间的随机数,使用了JavaScript内置的`Math.random()`方法。这个方法会返回一个大于等于0且小于1的随机浮点数。 第二个示例是生成一个指定范围内的随机整数。这个函数`getRandomInt(min, max)`使用了`Math.random()`方法生成一个0到1之间的随机浮点数,并且将其乘以(max - min + 1)来获得一个在指定范围内的随机浮点数。然后使用`Math.floor()`函数向下取整,以得到一个整数。最后返回这个随机整数。 以上就是使用JavaScript编写的产生随机数的程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值