本文介绍了Excel-VBA中数组中的随机元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我席卷了互联网,但仍未找到解决该问题的任何方法:

I've stormed the internet and still have not found any solution to this question:

我有一个类似的数组;

I have an array like so;

Dim PayRate As Variant
PayRate = Array(10, 20, 30)

Cells(i, "E").value = PayRate(Int((2 - 1 + 1) * Rnd + 1))

但是这只会给我20和30的数组(元素1和2),我也希望元素0 = 10在混合中?我该怎么做呢?

but this will only give me 20 and 30 from the array (elements 1 and 2), I also want element 0 = 10 in the mix ? How do I got about doing that?

推荐答案

这可以做到: Int(Rnd()* 3)+ 1

Rnd()从和返回一个均匀分布的随机数包括0到不包括1。

Rnd() returns a uniformly distributed random number from and including 0 to and not including 1.

重要:您必须具有 Option Base 1 在模块顶部,因此您的PayRate数组的最低边界为1。似乎您已经给了这个问题文本。

Important: you must have Option Base 1 at the top of the module so your PayRate array has lowest bound of 1. Seems like you have this given your question text.

这篇关于Excel-VBA中数组中的随机元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:18