随机字符串 [英] Random String
问题描述
任何人都可以建议如何创建一个生成
随机字符串的函数?
函数应该是:
int _RandomString(char ** str,int len);
所以,它需要一个空字符串str并且它放入一个随机字符串
lenght len,否则返回1表示成功0否则。
提前致谢,
最好的问候,
Andrea
" Andrea" < ac ****** @ gmail.com写信息
新闻:11 ********************* @ k79g2000hse。 googlegro ups.com ...
任何人都可以建议如何创建一个生成
$的函数b $ b随机字符串?
函数应该是:
int _RandomString(char ** str,int len);
所以,它需要一个空的字符串str并且它放入一个随机的字符串
lenght len,并且它返回1表示成功0否则。
在此先感谢,
最好的问候,
Andrea
希望这不是作业
(未经测试)
int randomstring(char ** str,int len)
{
char * answer = malloc(len + 1);
int i;
if(answer)
{
for(i = 0; i< len; i ++)
answer [i] = randchar();
answer [len] = 0 ;
}
* str = answer;
回答? 1:0;
}
/ *
这个由你决定。你是一个随意的角色是什么意思?
* /
char randchar()
{
char answer ;
do
{
answer = rand()%255;
}
while(!isalnum(ch));
返回答案;
}
Andrea写道:
任何人都可以建议如何创建一个生成<的函数br />
随机字符串?
C库没有RNG,只提供了PRNG,即使是蒙特卡罗模拟,也可能不是
,远远少于任何安全
计划。
这是什么随机字符串用于?
从计算机获取随机数是一个非常难的问题,并且除非你有一些熵源,否则无法完成
类。您的操作系统
可能会提供这样的随机源,例如某些Intel CPU的
附带一个内置RNG。
函数应该是:
int _RandomString(char ** str,int len);
所以,需要一个空字符串str,它放入一个随机字符串
lenght len,否则返回1表示成功0。
下划线前缀污染实施名称空间。
-
Tor< torust [at] online [dot] no>
Andrea说:
任何人都可以建议如何创建一个生成
随机字符串的函数?
函数应该是:
int _RandomString(char ** str,int len);
你不能拥有前导下划线,因为_RandomString是保留的
供实现使用。但是你可以使用RandomString,如果你喜欢
。
所以,它需要一个空字符串str并且它放入一个随机字符串
lenght len,否则返回1表示成功0。
这是你可以做到的一种方式:
#include< stdlib.h>
#include< time.h>
#include< stdio.h>
int RandomString(char ** str,int len)
{
int rc = 0; / * 1表示成功* /
size_t len_ = len;
if(str!= NULL)
{
* str = malloc(len_ + 1);
if(* str!= NULL)
{
const char sym [] =
" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
" \"%^& *()_- abcdefghijklmnopqrstuvwxyz"
" + = [] {} \\ |,<> /英寸;? / *我错过了吗? * /
size_t maxsym = sizeof sym / sizeof sym [0] - 1;
size_t i = 0;
while(i< len_)
{
int r = maxsym *(rand()/(RAND_MAX + 1.0));
(* str)[i ++ ] = sym [r];
}
(* str)[i] =''\ 0'';
rc = 1;
}
}
返回rc;
}
int main(无效)
{
int rc = 0;
int maxtrials = 20;
char * s = NULL;
srand((无符号)时间(0));
while(rc == 0&& maxtrials-- 0)
{
if(RandomString(& s,42))
{
printf("%s \\ \\ n,s);
免费;
}
}
返回0; < br $>
}
样本输出:
Gr ^ hlgopi> jcXhgr> Qv< + 0F /< L * tJ0WZsz3Y%rA5pu
i8WJ + TbhPXh * Wdr = R> [oXga" 0 / DA4] {m> M \ xh ^" \ = 3
XJgA6, 8 LT;Ĵ(bE\cD6mH< d {| + WR ^ KDV 8GuRx {Yr?TV * {
bX%o& A\ZowSae6?zJV |" BLK2k> 1?T({{{SfT& _uDP9
o {FnqZ4k + G} 7Ib4JbYq%Ogv {.KXqYo + 8e1wP%=。O3 =
VLYZ(.yIX9zF0vZX_z8T | mVp / xd,7gw ^ [G] TCodbyX
qyFCIn> Q3 {?Zg / IE | Qvpuj \ j?4TcgD.R> B& Fy)[R(
N,&)8Wx] 8d-0I-4c6kp3 / nE%[?] U = G = BAQiJo%9w1j
wJD = wK)_N * G%| 5%xP?NAf5UO)& 6_8] sMqb)J4upL
IiQk" r。1Xcg ^ \ {.W> *(+ IscuKvy1(6JAW {cA + / CKb
sn ^ jk + gAR ^ T519Qx,RP0c" X%/ - O> l-_ZI> 4 [xl?A9S
GB ^),WzJXWkwyjR9g / jAY = 7 ^ q {9q1J4IU)pP {baN | F
5szW [" V] cuUjJ7aTxbd = t, U * JK2t_<< eou> cHT4tAY
YU),oP(MCLG_r" stFMoA = X {z.8E0 = PZLk \ GTImhV | x
] kJhYZ {9ar_" ; c& -q * W2 {imxz11} jW" ^ L7m])8tgi(A
53eeu4.x< f * sZ)u | BDUeY%MO< U3b?am4eLiUQfDLGj
0gCvaN,{[TSBiN)mz(H)bwsFC5uQQ。< Rd788V2?KVR
V0f [mZT {\ {nl5 + q + C3x8VVG& * c)* w] wD] W2cxV ^ qMB
WS> 9EAC / I_TY / z> * PtU8 | J(< wORK0fWX | U)8 * k6nE"
7CM4idy / lsICn1" 1M%gjz-?Ri&> pDCBLOYQ |> A \ [B
第二个样本输出:
pxTxN< Y3Jr + ae,/ KYVd \o1F<%q)rdI& N1y6Pu(T?IF
axA_4-] h%eji-6T1x \KNGM8Nc [t} = / 7WvH} + rv ^ FU8
x] EC} /5BLMXTa.IO [5N。^ 4DT {} BT6( %{Vh2Mf8X = Uw
H {tZFh * ^ d,_rOYjZtp} HfMyiaZr,N7r * /(} D9WhmQA
Zfa40Pu \)VFR0psrk1yW)w [X.> udk6o6mKAna} f2Nv
UN)81C9 + -hjX0fUuE1 =] 7a9IIjAylXf1m8AnKJ-u = E
D = ti,a * xh0 \ zjb< 3ITjK1rUp> fKs%Y} OEZ | 8< X} dY
yYjUUmeoRyq4F%2umuH7m)L7ZU3yLgJ61 [%Vb16 [+ w
\ 1J< w\p ?? W] K * | fs = B / | C + V_XWeLJWEKgA2- + 2_Ja
to& TIHHTFB)2)0Z {fm9> = Ue / W8yZg3< UrYp.h | Qw4x
zcyL%Y,bWv \\ \\ .tO5eplhl2U6sRnli(q%QNLdql)N3OF
04 * 5iFrK + uf\_ | gFb7}?YFK>} \ * E.tT.xy2%?tm + -N
t3GUIs ^ 9r = O / zF {PU + 5ox] -zQ-f / UAM9D& * VH |(,uv
| g> x \ rk> 2 ^ + bOC6 {B" ; 0XbD [1A5, 20px | RvrJ9WHB + 4
nAHt0SG1rsFfuqlpslZcesSQ>%oEeaJNl" 2mtJn)/]
8sft ^ TZ]} 0hKRfn1uNc?bIZe} Fxfmw& ua4-?X<] P.(
bN7JP =)= + 36Mh /%" dAJ2 {u7Zt(UjwQASfId {56s \Az
EsxhEVrYYiE)3 | / Ycvqn9Q} nH> t。]] v4helw< ; XGT1U
= 50z& + gGaq_%ZpYE103%Tl20 ^> YjR0E)O0> BhDlTl
9tW- | 4y>)Dj-E7)erz1r0_21 * Djjz / Q,所以& mtNjL%N
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Hi,
Anyone could me suggest how to create a function that generates a
random string?
The function should be:
int _RandomString(char** str,int len);
so, it takes an empty string str and it puts in in a random string of
lenght len, and it returns 1 for success 0 otherwise.
Thanks in advance,
best regards,
Andrea
"Andrea" <ac******@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...Hi,
Anyone could me suggest how to create a function that generates a
random string?
The function should be:
int _RandomString(char** str,int len);
so, it takes an empty string str and it puts in in a random string of
lenght len, and it returns 1 for success 0 otherwise.
Thanks in advance,
best regards,
Andrea
Hope this isn''t homework
(untested)
int randomstring(char **str, int len)
{
char *answer = malloc(len + 1);
int i;
if(answer)
{
for(i=0;i<len;i++)
answer[i] = randchar();
answer[len] = 0;
}
*str = answer;
return answer ? 1 : 0;
}
/*
This one is up to you. What do you mean by a random character?
*/
char randchar()
{
char answer;
do
{
answer = rand() % 255;
}
while(! isalnum(ch));
return answer;
}
Andrea wrote:Hi,
Anyone could me suggest how to create a function that generates a
random string?The C library has no RNG, only a PRNG is provided, which may not be
usable for even a Monte-Carlo simulations, far less for any security
programs.
What shall this "random" string be used for?
Getting random numbers from a computer, is a quite hard problem, and
cannot be done unless you have an entropy source of some kind. Your OS
might provide such a source for randomness, for example some Intel CPU''s
comes with a build-in RNG.
The function should be:
int _RandomString(char** str,int len);
so, it takes an empty string str and it puts in in a random string of
lenght len, and it returns 1 for success 0 otherwise.Underscore prefix is polluting the implementation name space.
--
Tor <torust [at] online [dot] no>
Andrea said:
Hi,
Anyone could me suggest how to create a function that generates a
random string?
The function should be:
int _RandomString(char** str,int len);You can''t have the leading underscore, since _RandomString is reserved
for use by the implementation. But you can have RandomString if you
like.
so, it takes an empty string str and it puts in in a random string of
lenght len, and it returns 1 for success 0 otherwise.Here''s one way you could do it:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int RandomString(char **str, int len)
{
int rc = 0; /* 1 indicates success */
size_t len_ = len;
if(str != NULL)
{
*str = malloc(len_ + 1);
if(*str != NULL)
{
const char sym[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"\"%^&*()_-abcdefghijklmnopqrstuvwxyz"
"+=[]{}\\|,<.>/?"; /* did I miss any? */
size_t maxsym = sizeof sym / sizeof sym[0] - 1;
size_t i = 0;
while(i < len_)
{
int r = maxsym * (rand() / (RAND_MAX + 1.0));
(*str)[i++] = sym[r];
}
(*str)[i] = ''\0'';
rc = 1;
}
}
return rc;
}
int main(void)
{
int rc = 0;
int maxtrials = 20;
char *s = NULL;
srand((unsigned)time(0));
while(rc == 0 && maxtrials-- 0)
{
if(RandomString(&s, 42))
{
printf("%s\n", s);
free(s);
}
}
return 0;
}
Sample output:
Gr^hlgopi>jcXhgr>Qv<+0F/<L*tJ0WZsz3Y%rA5pu
i8WJ+TbhPXh*Wdr=R>[oXga"0/DA4]{m>M\xh^"\=3
XJgA6,8<j(bE\cD6mH<d{|+wr^KDV8GuRx{Yr?TV*{
bX%o&A\ZowSae6?zJV|"BLK2k>1?T({{{SfT&_uDP9
o{FnqZ4k+G}7Ib4JbYq%Ogv{.KXqYo+8e1wP%=.O3=
VLYZ(.yIX9zF0vZX_z8T|mVp/xd,7gw^[G]TCodbyX
qyFCIn>Q3{?Zg/IE|Qvpuj\j?4TcgD.R>B&Fy")[R(
N,&)8Wx]8d-0I-4c6kp3/nE%[?]U=G=BAQiJo%9w1j
wJD=wK)_N*G%|5%x"P?N"Af5UO)&6_8]sMqb)J4upL
IiQk"r."1Xcg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\Rk>2^+bOC6{B"0XbD[1a5,20px|RvrJ9WHB+4
nAHt0SG1rsFfuqlpslZcesSQ>%oEeaJNl"2mtJn)/]
8sft^TZ]}0hKRfn1uNc?bIZe}Fxfmw&ua4-?X<]P.(
bN7JP=)=+36Mh/%"dAJ2{u7Zt(UjwQASfId{56s\Az
EsxhEVrYYiE)3|/Ycvqn9Q}nH>t.]]v4helw<XGT1U
=5Oz&+gGaq_%ZpYElO3%Tl20^>YjR"0E)O0>BhDlTl
9tW-|4y>)Dj-E7)erz1r0_21*Djjz/Q,so&mtNjL%N
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!