Arrays 数组的Arduino语言洗牌顺序
我在和arduino一起工作,但我似乎不能洗牌。 问题是,对于arduino,我不能使用arraylist,这使得我很难洗牌数组 我真正想要的是一个从0到52的数字列表。所以每次我运行程序时,它都会以不同的顺序被洗牌,数字从0到52 这是我的密码:Arrays 数组的Arduino语言洗牌顺序,arrays,for-loop,random,arduino,shuffle,Arrays,For Loop,Random,Arduino,Shuffle,我在和arduino一起工作,但我似乎不能洗牌。 问题是,对于arduino,我不能使用arraylist,这使得我很难洗牌数组 我真正想要的是一个从0到52的数字列表。所以每次我运行程序时,它都会以不同的顺序被洗牌,数字从0到52 这是我的密码: const int MAXNUMMER = 52; int numbers[52]; int temp = numbers[first]; //int numbers[first] = numbers[second]; //int number
const int MAXNUMMER = 52;
int numbers[52];
int temp = numbers[first];
//int numbers[first] = numbers[second];
//int numbers[second] = temp;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomizeList();
}
void loop() {
// put your main code here, to run repeatedly:
}
void randomizeList()
{
randomSeed(analogRead(A0));
int r = random(53);
for(int i =0; i < MAXNUMMER; i++)
{
if(numbers[i] != r)
{
numbers[i] = r;
Serial.println(numbers[i]);
}
}
}
我测试了这个,它应该可以工作。我只是在函数中使用Serial.print,因为我想在最后看到所有的
const int NUMOFNUMBERS = 52;
int numbers[NUMOFNUMBERS];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomizeList();
}
void loop() {
// put your main code here, to run repeatedly:
}
void randomizeList()
{
unsigned char chosen[NUMOFNUMBERS];
unsigned char index, i2;
for (index = 0; index < NUMOFNUMBERS; index++)
chosen[index] = 0;
randomSeed(analogRead(A0));
for (index = 0; index < NUMOFNUMBERS; index++)
{
int r = random(NUMOFNUMBERS-index);
for (i2 = 0; i2 <= r; i2++)
{
r += chosen[i2];
}
chosen[r] = 1;
numbers[index] = r;
Serial.print(numbers[index]);
Serial.print(" ");
}
Serial.println("");
}
编辑:如果您想多次随机化列表,我建议您将randomSeed函数从函数移到setup函数。这样,您就可以随时调用randomizeList
还有一件事:如果您希望列表中的值为[1,52]而不是[0,51],则只需编辑行号[index]=r;成数[索引]=r+1 你的问题是。。。确切地说,我想要的是一个从0到52的数字列表。所以每次我运行程序时,它都会以不同的顺序被洗牌,数字从0到52
4 1 3 2 0
2 3 4 0 1
1 0 2 4 3
0 4 1 3 2
3 0 4 2 1
4 3 2 1 0