在c++srand中设置随机数的最大值和最小值

Setting Max and Min numbers for random in c++ srand

本文关键字:最大值 最小值 随机数 c++srand 设置      更新时间:2023-10-16

在我的程序中,我试图模拟轮盘旋转。目前我的设置是给我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