如何在C语言中生成两个数字之间的随机数?(内核)

如何在C语言中生成两个数字之间的随机数?(内核),c,random,operating-system,kernel,C,Random,Operating System,Kernel,我想在C(内核)中的两个数字之间生成随机数。在C中使用标准库很容易,但在没有库的C中,我只找到了以下链接:,我不知道如何使用链接中的代码 我不能显示我的代码。因为我的代码太长(+1000行代码) 编辑 这是我不完整的代码: #include <lib.h> int rand() { unsigned long int next = 1; next = next * 1103515245 + 12345; return (unsigned int)(next

我想在C(内核)中的两个数字之间生成随机数。在C中使用标准库很容易,但在没有库的C中,我只找到了以下链接:,我不知道如何使用链接中的代码

我不能显示我的代码。因为我的代码太长(+1000行代码)

编辑

这是我不完整的代码:

#include <lib.h>

int rand()
{
    unsigned long int next = 1;
    next = next * 1103515245 + 12345;
    return (unsigned int)(next / 65536) % (RAND_MAX + 1);
}

int rrand(int min, int max)
{
    /* incomplete */
}
#包括
int rand()
{
无符号长整数next=1;
next=next*1103515245+12345;
返回值(无符号整数)(next/65536)%(RAND_MAX+1);
}
整数r兰德(整数最小值,整数最大值)
{
/*残缺的*/
}
lib.h:

#ifndef _LIB_H
# define _LIB_H
#endif

#ifdef _LIB_H
# ifndef _DEF_H /* this line isn't important for this question */
#  include <def.h> /* this line isn't important for this question */
# endif

# define RAND_MAX 32767
extern int rand();
extern int rrand(int min, int max);
extern void *malloc(size_t size); /* this line isn't important for this question */
extern int atoi(char *str); /* this line isn't important for this question */
extern char *itoa(int value, char *str, int base); /* this line isn't important for this question */
#endif
\ifndef\u LIB\H
#定义_LIB_H
#恩迪夫
#ifdef_LIB_H
#如果ndef_DEF_H/*这一行对于这个问题并不重要*/
#include/*这一行对于这个问题并不重要*/
#恩迪夫
#定义RAND_MAX 32767
extern int rand();
外部内部rrand(内部最小值,内部最大值);
外部空隙*malloc(尺寸);/*这句话对这个问题不重要*/
外部内部原子(char*str);/*这句话对这个问题不重要*/
外部字符*itoa(int值,字符*str,int基);/*这句话对这个问题不重要*/
#恩迪夫
编辑2


我可以编译我的代码而不会出错。

我通常使用一个函数来生成一个随机数,该随机数不超过
n

unsigned randto(unsigned n) {
    unsigned r, hi = (RAND_MAX / n) * n; // avoid
    do r = rand(); while (r >= hi);      // bias
    return r % n;
}
使用此函数(当然还有,
rand()
),您可以轻松编写
rrand()


链接中的问题/答案有什么问题?@Jabberwocky我无法使用问题中的答案生成随机数。我不知道如何使用问题中的答案生成随机数,例如5到25。@Jabberwocky我的问题中有任何问题需要编辑吗?那么为什么您不能使用链接中建议的代码?你试了什么?你不能编译代码吗?它是否运行,但您得到的结果不是您期望的结果?阅读以下内容:和:。如果您正在为生成伪随机数的函数和从某个范围中选择的函数使用1000多行代码,那么您做得太多了。assert()函数在哪里?assert不需要,已删除。。。但是不要去调用
rrand(100,10)错误:“嗨”未声明(此函数首次使用)复制/粘贴错误:-)对不起,我忘记了一行。
unsigned rrand(unsigned lo, unsigned hi) {
    return lo + randto(hi - lo + 1);
}