C++_生成随机字符串

#include<iostream>
#include<ctime>
using namespace std;

const int LEN_NAME=10;

char *rand_str(char *str,const int len)
{
    int i;
    for(i=0;i<len;++i)
        str[i]='A'+rand()%26;
    str[i+1]='\0';
    return str;
}

void main()
{
    srand((unsigned)time(NULL));
    char name[LEN_NAME+1];
    cout<<rand_str(name,LEN_NAME)<<endl;
}

若出现Stack around the variable ‘name’ was corrupted,就把project->配置属性->c/c++->代码生成->基本运行时检查改为默认值。

posted @ 2013-03-21 14:39  开心成长  阅读(1866)  评论(0编辑  收藏  举报