2012-12-05 49 views
0

我想從一個空數組開始,然後使用下面的代碼生成一個隨機數字模式,但我似乎無法讓它工作。如何添加到陣列在Arduino

int sequence = {}; 

random(1, 4); 

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

回答

1

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); 
0

這不是一個數組。
是一個數組,int sequence[7];

0

這就是我要做的,它在我的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; 
} 
}