1

我试图从一个空数组开始,然后使用以下代码生成一个随机数模式,但我似乎无法让它工作。

 int sequence = {};

 random(1, 4);

 for (int i=0; i <= 7; i++){
 sequence[i] = random(1, 4);
 } 
4

3 回答 3

3

Arduino 基于 C++。

int sequence[8];

// You must initialize the pseudo-random number generator
// You can get a "random seed" using analogRead(0) if this
// pin isn't been used and unplugged.
randomSeed(analogRead(0));

for (int i=0; i <= 7; i++){
    sequence[i] = random(1, 4);
于 2012-12-05T17:40:38.717 回答
1

那不是数组。
是一个数组,int sequence[7];

于 2012-12-05T17:38:58.590 回答
1

我就是这样做的,它在我的 arduino IDE 上编译得很好。我认为没有必要播种 rand 函数,除非您的应用程序需要一个真正的随机数(不可重复)。

https://www.arduino.cc/en/reference/random应该可以帮助您

void loop() {
 int sequence[8]; // this is an array

 for (int i=0; i <= 7; i++){
 // use modulo to get remainder, add 1 to make it a range of 1-4
 sequence[i] = rand() % 4 + 1;
 }
}
于 2017-04-25T00:45:38.110 回答