如何为不同类型的2d数组生成随机数
How to generate a random number for 2d array of a different type?
彩色类型的2d阵列
enum color{black, white};
color A[nrow][ncol];
我需要一个随机数生成器,使[I][j]具有"0"或"1",然后我会说:
if (A[i][j]==0)
{
A[i][j]=black;
}
事情是,我们主要写:
srand(unsigned int (NULL);
所以当我写:
for (int i=0; i<nrow; i++)
{
for (int j=0; j<ncol; j++)
{
A[i][j]= rand () % 2;
}
一个错误表明int不能分配给类型color。如何解决这个问题?
一个选项是将int转换为彩色
A[i][j]= (color)(rand () % 2);
其他选项是
A[i][j]= (rand () % 2) ? white : black;
顺便说一句,我更喜欢
A[i][j]= (rand () & 1) ? white : black;
相关文章:
- if数组上的随机数
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 具有随机数的二维数组不会更改
- 如何在 c++ 中从数组中选择一个随机数?
- C++ 用于在数组中打印字符串的随机数
- 如何防止每次 Arduino 启动时颜色数组都是相同的随机数组
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 需要关于如何将我的随机数数组连接到冒泡排序的帮助
- 如何对0,1,..中的k个随机数进行采样..,n-1,而不缓存到数组
- 使用数组生成随机数
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组初始化为所有元素为零,吐出随机数
- 生成随机数并将它们放入数组中
- 如何在C 中创建正常分布式随机数生成器的数组
- 将范围随机数保存到数组而不重复的函数
- 使用具有唯一随机数的结构数组创建多个对象
- 用非重复的随机数填充 2D 数组
- 随机数生成器C++动态数组
- 在多维数组中生成唯一的随机数C++
- 将 2D 数组随机数生成器传递到主C++