Random Number Series Questions

This webpage gives a perfect solution to generate K random numbers from given N size array: http://www.geeksforgeeks.org/reservoir-sampling/

However, there are a varieties of related questions.

1: implement an algorithm that takes as input an array of distinct element and a size, return a subset of the given size of the array elements. All subset should be equally likely. Return the result in input array itself.

int randomGenerator(int left, int right) {
  srand(time(NULL));
  return left + rand() % (right - left + 1);
}

// given a size of array and input k, return k subset random numbers.
void randomK(vector<int>& nums, int k) {
  default_random_engine seed((random_device())());
  for(int i = 0; i < k; ++i) {
    swap(nums[i], nums[uniform_int_distribution<int>{i, static_cast<int>(nums.size()) - 1}(seed)]);
  }
  return;
}

int main(void) {
  vector<int> nums{0, 1, 2, 3, 4, 5, 6};
  randomK(nums, 3);
  for(int i = 0; i < 3; ++i) {
    cout << nums[i] << " ";
  }
  cout << endl;
}


Randomly return any maximum index. Suppose in the array, we have many maximum values, randomly return one of the indexes. We can also apply the reservoir sampling technique here.

1: The first though is to count the maximum values numbers, generate a random number for this count, traverse the array again and return the random index.

2: we can make use of the reservoir sampling.

#include "header.h"
using namespace std;

// in an unsorted array, there are many maximum values, randomly return one index.

int randomMaximumValueIndex(vector<int>& nums) {
  srand(time(NULL));
  int maxValue = 0;
  int count = 0;
  int res = 0;
  for(int i = 0; i < nums.size(); ++i) {
    if(nums[i] > maxValue) {
      maxValue = nums[i];
      count++;
      res = i;
    } else if(nums[i] == maxValue){
      if(rand() % ++count == 0) res = i; // if found the other maxValue, randomly update the index.
    }
  }
  return res;
}



int main(void) {
  vector<int> nums{0, 1, 6, 3, 6, 5, 6};
  cout << randomMaximumValueIndex(nums) << endl;

}

### 回答1: Simulink中的随机数模块是一种用于生成随机数的模块。它可以用于模拟随机事件,例如噪声、信号失真等。该模块可以生成不同分布的随机数,例如均匀分布、正态分布、泊松分布等。用户可以根据需要设置随机数的范围、均值、方差等参数。在Simulink中,随机数模块通常与其他模块一起使用,例如滤波器、控制器等,以模拟实际系统中的随机事件。 ### 回答2: Simulink的随机数模块(Random Number Module)是一种可以在模型中生成随机数的模块。这个模块可以用来模拟各种不确定因素对系统的影响,例如噪声、随机变化等。 在 Simulink 中,随机数模块有多种不同的类型和参数设置可以使用。其中最常用的是 Uniform Random Number Generator 模块,它可以生成一个在指定范围内的均匀分布随机数。这个模块的参数包括随机数的范围、种子等。还有其他的随机数模块,如 Gaussian Random Number Generator 模块,用于生成高斯分布随机数。 随机数模块可以用于多种场合。例如,在嵌入式系统的开发中,我们可能需要使用随机数模块来生成一些测试数据,以测试代码的正确性和可靠性。另外,在模拟系统时,我们可以使用随机数模块来模拟各种不确定的因素,从而更好地理解系统的运行情况和性能。 需要注意的是,随机数模块的使用应该遵循一定的规则和方法。首先,我们需要选择合适的随机数分布类型和参数设置,来模拟真实情况下的随机变化。其次,我们需要设置随机数的种子,以确保每次模拟时生成的随机数序列是固定的,这样才能保证模拟结果的可重现性和可靠性。最后,我们需要在模拟过程中进行数据分析和统计,以对随机变量的分布和统计特性进行分析和验证。 在总体上,随机数模块是一种非常有用的工具,可以帮助我们更好地理解和设计系统。使用正确的随机数模块和方法,可以提高我们的模拟效率和准确性,从而更好地实现系统开发和优化。 ### 回答3: Simulink中的Random Number模块是用于生成随机数的模块,可以用于各种模拟和测试环境中。 在Simulink中,Random Number模块有两种类型:Unifrom Random Number和Normal Random Number。其中,Uniform Random Number模块用于生成均匀分布的随机数,而Normal Random Number模块用于生成正态分布的随机数。 在使用Random Number模块时,可以根据需要设置随机数生成器的种子、均值、标准差和范围等参数,以便生成满足特定要求的随机数序列。同时,还可以设置生成随机数的时间间隔,以控制随机数的输出频率。 在Simulink中,Random Number模块广泛用于各种模拟和测试场景中,如随机噪声生成、随机信号源生成、Monte Carlo仿真等等。随机数的应用不仅可以提高系统模拟的灵活性和多样性,还可以提高仿真的准确性和可靠性。 总之,Simulink中的Random Number模块是非常实用的工具,可以帮助工程师和科学家在各种模拟和测试场景中快速生成符合要求的随机数序列,从而提高仿真的可信度和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值