在C中重复生成相同值的随机字符串和数字
我试图用C实现AVL树。我在每个节点上存储一个随机整数和一个随机字符串。我更改了代码,得到了随机字符串。但现在我有另一个问题了。在顺序遍历期间,我得到与输出相同的字符串 有人能查一下我的密码吗在C中重复生成相同值的随机字符串和数字,c,random,avl-tree,C,Random,Avl Tree,我试图用C实现AVL树。我在每个节点上存储一个随机整数和一个随机字符串。我更改了代码,得到了随机字符串。但现在我有另一个问题了。在顺序遍历期间,我得到与输出相同的字符串 有人能查一下我的密码吗 #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> time_t t; struct node {
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
time_t t;
struct node {
int val;
char *str;
struct node *left_child;
struct node *right_child;
};
struct node* LL_rotation(struct node *root) {
struct node *children;
children = root -> left_child;
root -> left_child = children -> right_child;
children -> right_child = root;
return children;
}
struct node* RR_rotation(struct node *root) {
struct node *children;
children = root -> right_child;
root -> right_child = children -> left_child;
children -> left_child = root;
return children;
}
struct node* LR_rotation(struct node *root) {
struct node *children;
children = root -> left_child;
root -> left_child = RR_rotation(children);
return LL_rotation(root);
}
struct node* RL_rotation(struct node *root) {
struct node *children;
children = root -> right_child;
root -> right_child = LL_rotation(children);
return RR_rotation(root);
}
int height(struct node *target) {
int h = 0, lh, rh, maxh;
if (target != NULL) {
lh = height(target -> left_child);
rh = height(target -> right_child);
if (rh > lh) {
maxh = rh;
} else {
maxh = lh;
}
h = maxh + 1;
}
return h;
}
int height_difference(struct node *target) {
int lh, rh, b;
lh = height(target -> left_child);
rh = height(target -> right_child);
b = lh - rh;
return b;
}
struct node* decide_rotation(struct node *target) {
int diff = height_difference(target);
int res;
if (diff > 1) {
res = height_difference(target -> left_child);
if (res > 0) {
target = LL_rotation(target);
} else {
target = LR_rotation(target);
}
} else if (diff < -1) {
res = height_difference(target -> right_child);
if (res > 0) {
target = RL_rotation(target);
} else {
target = RR_rotation(target);
}
}
return target;
}
struct node* insert(struct node *target, int data, char *info) {
if (target == NULL) {
target = (struct node*) malloc(sizeof(struct node));
target -> val = data;
target -> str = info;
target -> left_child = NULL;
target -> right_child = NULL;
return target;
} else if (data > target -> val) {
target -> right_child = insert(target -> right_child, data, info);
target = decide_rotation(target);
} else if (data < target -> val) {
target -> left_child = insert(target -> left_child, data, info);
target = decide_rotation(target);
}
return target;
}
void ioTraverse(struct node *tree) {//in-order traversal
if (tree == NULL) {
return;
} else {
ioTraverse(tree -> left_child);
printf("%d %s\n", tree -> val, tree -> str);
ioTraverse(tree -> right_child);
}
}
void random_string_generator(char foo[], int length) {
int i = 0;
length--;
while (length--) {
foo[i] = rand() % 94 + 33;
i++;
}
return;
}
int rand_range(int min, int max) {
return rand() % (max - min + 1) + min;
}
int main() {
srand(0);
printf("Enter the no. of elements:\n");
int n, ch, i, val, idx = 1;
scanf("%d", &n);
struct node *tree = NULL;
for (i = 0; i < n; i++) {
char res[123];
random_string_generator(res, 6);
printf("%s\n", res);
}
for (i = 0; i < n; i++) {
char foo[123];
val = rand_range(idx, idx + 100);
//scanf("%d", &val);
random_string_generator(foo, 6);
//scanf("%s", &foo);
printf("%s\n", foo);
tree = insert(tree, val, foo);
idx += rand_range(200, rand_range(240, 260));
//ioTraverse(tree);
}
ioTraverse(tree);
return 0;
}
#包括
#包括
#包括
#包括
#包括
时间;
结构节点{
int-val;
char*str;
结构节点*左_子节点;
结构节点*右\子节点;
};
结构节点*LL_旋转(结构节点*根){
结构节点*子节点;
children=root->left\u child;
根->左\u子对象=子对象->右\u子对象;
children->right\u child=root;
返回儿童;
}
结构节点*RR_旋转(结构节点*根){
结构节点*子节点;
children=root->right\u child;
根->右\u子对象=子对象->左\u子对象;
children->left_child=root;
返回儿童;
}
结构节点*LR_旋转(结构节点*根){
结构节点*子节点;
children=root->left\u child;
根->左\u子对象=右\u旋转(子对象);
返回LL_旋转(根);
}
结构节点*RL_旋转(结构节点*根){
结构节点*子节点;
children=root->right\u child;
根->右\u子对象=左\u旋转(子对象);
返回RR_旋转(根);
}
整数高度(结构节点*目标){
int h=0,左侧,右侧,最大值;
如果(目标!=NULL){
lh=高度(目标->左侧儿童);
rh=高度(目标->右侧子对象);
如果(右侧>左侧){
maxh=rh;
}否则{
maxh=lh;
}
h=maxh+1;
}
返回h;
}
整数高度差(结构节点*目标){
内左,右,b;
lh=高度(目标->左侧儿童);
rh=高度(目标->右侧子对象);
b=左侧-右侧;
返回b;
}
结构节点*决定旋转(结构节点*目标){
int diff=高度差(目标);
国际关系;
如果(差异>1){
res=高度差(目标->左子项);
如果(分辨率>0){
目标=LL_旋转(目标);
}否则{
目标=LR_旋转(目标);
}
}否则如果(差异<-1){
res=高度差(目标->右侧子对象);
如果(分辨率>0){
目标=RL_旋转(目标);
}否则{
目标=RR_旋转(目标);
}
}
回报目标;
}
结构节点*插入(结构节点*目标,整型数据,字符*信息){
if(target==NULL){
target=(结构节点*)malloc(sizeof(结构节点));
目标->价值=数据;
目标->str=info;
target->left_child=NULL;
target->right\u child=NULL;
回报目标;
}否则如果(数据>目标->val){
target->right\u child=插入(target->right\u child,数据,信息);
目标=决定旋转(目标);
}否则如果(数据<目标->值){
target->left_child=插入(target->left_child,数据,信息);
目标=决定旋转(目标);
}
回报目标;
}
void ioTraverse(结构节点*树){//按顺序遍历
if(tree==NULL){
返回;
}否则{
ioTraverse(树->左侧子对象);
printf(“%d%s\n”,tree->val,tree->str);
ioTraverse(树->右侧子对象);
}
}
无效随机字符串生成器(字符foo[],整数长度){
int i=0;
长度--;
while(长度--){
foo[i]=rand()%94+33;
i++;
}
返回;
}
整数随机范围(整数最小值,整数最大值){
返回rand()%(max-min+1)+min;
}
int main(){
srand(0);
printf(“输入元素编号:\n”);
int n,ch,i,val,idx=1;
scanf(“%d”和“&n”);
结构节点*tree=NULL;
对于(i=0;i
我认为这与传递给srand()方法的种子有关
time()方法以秒为单位返回当前时间,因此在程序执行期间,
time(0)+getpid()
始终保持不变,因此它将在rand()
函数中生成相同的整数。您调用的srand()
太频繁了。在大多数情况下,每个程序一次就足够了。请扩展@JonathanLeffler的评论:如果您在同一日历秒内调用random\u string\u generator()
两次,您将获得相同的随机种子,从而从rand()
获得相同的随机数序列。谢谢,我将相应地更改代码。我更改并获得随机字符串。但现在我有另一个问题了。在顺序遍历期间,我得到与输出相同的字符串。有什么建议吗?你在对所有字符串重复使用相同的内存char foo[123]
被重复覆盖。您需要分别为每个字符串分配malloc()
内存。是的,它确实与调用srand()
的方式有关。然而,要成为一个有价值的解释,您需要解释的不仅仅是“与传递给srand()
”的种子有关的事情”。不过,这是一个不错的评论。。。