如何将唯一名称随机分配给C中的一系列数字 [英] How to randomly assign unique name to a series of number in C

查看:119
本文介绍了如何将唯一名称随机分配给C中的一系列数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have generated a series of number , how to randomly assign name to it from a set of name. Let say I have, Vertex [i]: n,
Vertex [0]: 1
Vertex [1]: 5
Vertex[2]: 9
.
.
.
Vertex[19]: 13

and a set of name : { apple, orange, papaya, watermelon, pineapple}

And what I want to do is when I key in 0 ( i value) which is for the first item, it will print 
Vertex [0]: 1 { orange } 
means it will pick one name from the list and random assign to n 





我的尝试:





What I have tried:

printf("=======================\n\n");
    printf("Number of Edges for each node:\n");
    for( i = 0 ; i < N ; i++ )
    {
        printf("Vertex %d: ",i);
        n=0;
        for( j = 0 ; j < N ; j++ )
        {
            if(graph[i][j])
                n+=1;  ////count the number of repeat
        }

        if(n>max)max=n;
        if(n<min)min=n; printf("%d="" ",="" n);="" printf("\n"); 

推荐答案

您必须构建一个包含所有可能名称的名称列表,并使用一些修改后的随机值来使用名称。如果选择了名称,请从该列表中删除它。所以它将是独一无二的。
You must build a name list with all possible names and than use some modified random value to use a name. And if the name is picked, delete it from that list. So it will be unique.


这篇关于如何将唯一名称随机分配给C中的一系列数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆