在c++srand中设置随机数的最大值和最小值
Setting Max and Min numbers for random in c++ srand
在我的程序中,我试图模拟轮盘旋转。目前我的设置是给我38个随机数,但我得到的所有这些数字都是max int 32767
我想我把max and min
设置错了,但我不知道怎么设置的。
我制作了这样的程序,效果很好,所以我真的很想了解我在这里缺少了什么。
我想知道这是否与我的功能有关,即在创建随机数后显示随机数。
我的代码如下,
int spin = 1;
int board;
double_clist cdl;
void Randomize() {
srand( (unsigned)time( NULL ) ) ; }
int Random(int Max) { return (rand() % Max) + 1; }
int main() {
int spin;
Randomize();
for(spin = 1; spin <= 38; spin++) {
for (spin = 0; spin < 38; spin++) {
board = Random(38);
cdl.insert_begin();
}
}
cdl.display();
这是我在列表中插入每个数字的功能,
void double_clist::insert_begin()
{
int value;
struct node *temp;
temp = create_node(value);
if (start == last && start == NULL)
{
cout<<"Element inserted in empty list"<<endl;
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
temp->next = start;
start->prev = temp;
start = temp;
start->prev = last;
last->next = start;
cout<<"Element inserted"<<endl;
}
}
这是我创建列表后显示列表的功能,
void double_clist::display()
{
int i;
struct node *s;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to display"<<endl;
return;
}
s = start;
for (i = 0;i < counter-1;i++)
{
cout<<s->info<<"<->";
s = s->next;
}
cout<<s->info<<endl;
}
随机数函数很好-必须是数据结构中的某个函数。
#include <iostream>
using namespace std;
void Randomize() {
srand( (unsigned)time( NULL ) ) ; }
int Random(int Max) { return (rand() % Max) + 1; }
int main() {
for (int i = 0; i < 20; i++)
{
int val = Random(38);
cout << val << " ";
}
return 0;
}
输出
Success time: 0 memory: 3296 signal:0
22 35 4 4 30 28 11 31 22 8 21 10 21 30 2 11 27 37 25 29
相关文章:
- 最大和最小 1 C++函数
- txt 文件中浮点数的最大和最小值
- 为什么我的C 代码无法计算整数的最大和最小值
- 函数读取最大和min int值,并用文本字符串返回
- 在C弦C 中收集最大和最小的数字
- 在数组C 中排序最大和最小的值
- 在数组中找到最大和最小的数字
- 查找序列中最大和最小的 2 个整数
- 在C++中使用 for 循环从用户输入中查找最大和最小整数
- 如何获得C 中向量复合物值的最大,最小和位置
- 我试图显示我输入的三个值中最大和最小的数量
- 给定有向图和商店位置,放置商店,使从任何城市到最近商店的最大距离最小化
- 我无法弄清楚如何在不使用 climits 的情况下计算最大和最小有符号数C++
- 使用 c++ 在值数组中查找最大利润(最大 - 最小值)
- 所有最接近的较小值
- 如何查找向量中元素的最大和最小索引
- C++数组从最大到最小的数字
- 程序可以在不使用数组的情况下找到5个数字中最大和最小的
- 如何用最少的比较得到3个值中的最大和最小值
- C++的平均、最大和最小赋值