c++ - Arduino 从给定数组中选择一个随机词
问题描述
我有这段代码可以在 LCD 上打印一个单词:
void setup(){
lcd.print("Hello World");
}
我希望它从这个数组中选择一个随机词:
const char *words[] = {"Happy","Sad","Angry","Worried","Shy","Excited","Suprised","Silly","Jealous","Hurt","Cold","Hot","Energized","Bored","Sick","Love","Upset"};
所以基本上使用这个数组,我希望它从这个数组中选择一个随机单词,然后使用 lcd.print 方法将它显示在 LCD 上。有任何想法吗?对不起,如果答案很明显,我只是 Arduino 的初学者。
解决方案
首先找到数组的长度,然后用它random
来获得一个达到该界限的数字:
const int len = sizeof(words) / sizeof(words[0]);
lcd.print(words[random(len)]);
推荐阅读
- angular - 无法使用 Angular 7 将正确的值附加到同一行的下拉列表中
- apache - 如何使用 htacess 重写 url
- android - 打开任何布局文件时,Android Studio 崩溃
- php - 如何在 PHP 中获取另一个类中设置的 Class 属性
- wordpress - 如何强制用户在 input type='tel' Wp Contact Form 7 Plugin 中仅输入数字,就像 Gravity Form 插件一样
- r - 有没有办法在 R 函数中为数据框中的每个子集赋予不同的对象名称?
- python - 抓取脚本提供的 JSON 并转换为数据框
- if-statement - Google 表格在指定时间之前和之后的工作时间
- python - 使用 .bat 文件中的“调用”to.py 脚本时出错
- odoo - 单击保存按钮后odoo服务器错误