本文目录一览:
- 1、求文件加解密思想,用C语言中的rand()函数实现
- 2、用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数
- 3、c语言如何实现随机数字的产生
- 4、如何用C语言生成[0.01,2]之间符合正态分布的随机数。。。注意是正态分布!!答案采用后再追加50分
- 5、c语言里 random函数怎么实现的
- 6、c语言程序设计-跳动的三角形
求文件加解密思想,用C语言中的rand()函数实现
最简单的加密思想:用rand()产生一个随机数,然后将文件中的每个字符依次与这个随机数进行异或,解密时只需要再进行异或运算即可。
原理 (a^b)^b = a (^为异或运算)
用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数
下面共有两个程序,程序2 加入了图形显示
程序1
这个程序就是你要的。
# include "stdio.h"
# include "math.h"
# include "stdlib.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000 /*这个值为N可以定义的最大长度*/
# define N 100 /*产生随机序列的点数,注意不要大于MAX_N*/
/*产生均匀分布的随机变量*/
void randa(float *x,int num);
/*产生瑞利分布的随机变量*/
void randr(float *x,int num);
/*产生标准高斯分布的随机变量*/
void randn(float *x,int num);
/*产生莱斯分布的随机变量*/
void randl(float *x, float a, float b, int num);
void fshow(char *name,float *x,int num);
main()
{
float x[N];
int i;
/*
randa(x,N);
randr(x,N);
randl(x,10,10,N);
*/
randn(x,N);
/*此时x[N]就是所需要的高斯分布的序列*/
/*显示该序列*/
fshow("x",x,N);
getch();
}
void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}
void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("n");
printf(name);
printf(" = ");
L=6;
/*按照每行6个数据的格式显示*/
for(i=0;inum;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("n");
if(x[i]0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}
程序 2
以下程序加入了图形显示的效果,因此更加直观,你可以参考一下。
/* 作者 Leo_nanjing
时间 2008.5.10
功能 生成各种分布的随机变量,并显示
*/
# include "stdio.h"
# include "math.h"
# include "graphics.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000
# define N 1000
void randa(float *x,int num);
void randr(float *x,int num);
void randn(float *x,int num);
void randl(float *x, float a, float b, int num);
void fshow(char *name,float *x,int num);
/*用于图形显示的部分*/
void init_graphic(unsigned color);
void plotxy(float *x, float *y, int num,int mode);
void plot(float *y,int num, int mode);
float max(float *x, int num);
float min(float *x, int num);
/*画出该随机序列的分布函数曲线*/
void plotpdf(float *x,int num,int part,int mode);
main()
{
float x[N];
int i;
randn(x,N);
fshow("x",x,N);
getch();
/*以下为图形显示部分*/
init_graphic(0);
/*显示随机序列*/
plot(x,N,1);
getch();
/*显示其分布函数*/
plotpdf(x,N,20,0);
getch();
}
void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}
void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("n");
printf(name);
printf(" = ");
L=6;
for(i=0;inum;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("n");
if(x[i]0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}
/*以下为图形显示的函数*/
void init_graphic(unsigned color)
{
int graphicdriver,graphicmode;
graphicdriver=DETECT;
graphicmode=1;
initgraph(graphicdriver,graphicmode,"E:\turboc2\");
setbkcolor(color);
}
void plotxy(float *x, float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
clrscr(0);
cleardevice();
setbkcolor(0);
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;inum;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("nn");
printf("%f",max_y);
printf("nnnnnnnnnn");
printf("nnn");
printf("%f",(max_y+min_y)/2);
printf("nnnnnnnnnn");
printf("nn");
printf("%f",min_y);
printf("n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);
}
void plot(float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
float x[MAX_N];
clrscr(0);
cleardevice();
setbkcolor(0);
for(i=0;inum;i++) x[i]=i+1;
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;inum;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("nn");
printf("%f",max_y);
printf("nnnnnnnnnn");
printf("nnn");
printf("%f",(max_y+min_y)/2);
printf("nnnnnnnnnn");
printf("nn");
printf("%f",min_y);
printf("n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);
}
void plotpdf(float *x,int num,int part,int mode)
{
int i,j;
float max_x,min_x,round,deltax,up,down,sum;
float xl[MAX_N],yl[MAX_N];
sum=0;
max_x=max(x,num);
min_x=min(x,num);
round=max_x-min_x;
deltax=round/part;
xl[0]=min_x;
for(i=1;i=part;i++)
{
xl[i]=min_x+deltax*i;
yl[i-1]=0;
up=xl[i];
down=xl[i-1];
for(j=0;jnum;j++)
{
if((x[j]up) (x[j]=down)) yl[i-1]=yl[i-1]+1;
}
yl[i-1]=yl[i-1]/num/deltax;
}
for(i=0;ipart;i++) sum=sum+yl[i];
plotxy(xl,yl,part,mode);
}
float max(float *x, int num)
{
int i;
float max;
max=x[0];
for(i=1;inum;i++)
{
if(x[i]max) max=x[i];
}
return max;
}
float min(float *x, int num)
{
int i;
float min;
min=x[0];
for(i=1;inum;i++)
{
if(x[i]min) min=x[i];
}
return min;
}
c语言如何实现随机数字的产生
数学意义上的随机数在计算机上已被证明不可能实现。通常的随机数是使用随机数发生器在一个有限大的线性空间里取一个数。“随机”甚至不能保证数字的出现是无规律的。
我觉得你的程序逻辑似乎不对,看程序a的值应该来自数组num[],假如在第一个for循环中生成的x值为1,第二次仍然生成1,程序将陷入死循环,又或者a是某个特定值,只是你应该给出说明。
使用系统时间作为随机数发生器是常见的选择,参考下面的随机输出1个1~99数字的程序:
#include
#include
#include
int
main(void)
{
int
i;
time_t
t;
srand((unsigned)
time(t));
printf("ten
random
numbers
from
to
99nn");
for(i=0;
i10;
i++)
printf("%dn",
rand()
%
100);
return
0;
}
如何用C语言生成[0.01,2]之间符合正态分布的随机数。。。注意是正态分布!!答案采用后再追加50分
# include stdio.h
# include math.h
# include stdlib.h
# include time.h
# define MAX_N 3000 /*这个值为N可以定义的最大长度*/
# define N 100 /*产生随机序列的点数,注意不要大于MAX_N*/
# define PI 3.141592653
void randn(double *x,int num)
{
double x1[MAX_N],x2[MAX_N];
int i;
srand((unsigned)time(NULL));
for(i=0;iN;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/(RAND_MAX+1);
x2[i]=x2[i]/(RAND_MAX+1);
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*2*PI);
}
}
void main()
{
double x[N],x_min,x_max;
int i;
FILE *fp;
if((fp=fopen("test.txt","w+"))==NULL)
{
fprintf(stderr,"Can't open the filen");
exit(1);
}
randn(x,N);
x_min=x[0];
x_max=x[0];
for(i=0;iN;i++)
{
if(x[i]x_max)
{
x_max=x[i];
}
if(x[i]x_min)
{
x_min=x[i];
}
}
for(i=0;iN;i++)
{
x[i]=(x[i]-x_min)/(x_max-x_min)*(2-0.01)+0.01;
}
for(i=0;iN;i++)
{
printf("%ft",x[i]);
fprintf(fp,"%lft",x[i]);
if(i%5==4)
{
printf("n");
}
}
if(fclose(fp)==EOF)
{
printf("Closing errorn");
}
}
把生成的数据放入txt文件中,再导入matlab中,查看是否符合正态分布。
matlab中用normplot()画图如下:
很接近红线,说明很符合正态分布。
再用以下代码进行精确性分析:
得到H1=0,说明确实是正态分布。。。。
c语言里 random函数怎么实现的
1.
rand函数是根据一个数(我们可以称它为种子)为基准,以某个递推公式推算出来的一系数,当这系列数很大的时候,就符合正态公布,从而相当于产生了随机数,
2.
但这产生的并不是真意正义上的随机数,是一个伪随机数,当计算机正常开机后,这个种子的值是定了的,除非你破坏了系统,为了改变这个种子的值。
3.
种子相同,产生的随机序列相同。这样做的好处是,方便我们产生一组固定的随机序列,用来调试程序。
4.
C提供了srand()函数,用来设置种子,它的原形是void
srand(
int
a)。
5.
在调用rand函数产生随机数前,应该先利用srand()设好随机数种子,如果未设随机数种子,默认种子为1。
c语言程序设计-跳动的三角形
clear all
close all
%channel system order
sysorder = 5 ;
% Number of system points
N=2000;
inp = randn(N,1);
n = randn(N,1);
[b,a] = butter(2,0.25);
Gz = tf(b,a,-1);
%This function is submitted to make inverse Z-transform (Matlab central file exchange)
%The first sysorder weight value
%h=ldiv(b,a,sysorder)';
% if you use ldiv this will give h :filter weights to be
h= [0.0976;
0.2873;
0.3360;
0.2210;
0.0964;];
y = lsim(Gz,inp);
%add some noise
n = n * std(y)/(10*std(n));
d = y + n;
totallength=size(d,1);
%Take 60 points for training
N=60 ;
%begin of algorithm
w = zeros ( sysorder , 1 ) ;
for n = sysorder : N
u = inp(n:-1:n-sysorder+1) ;
y(n)= w' * u;
e(n) = d(n) - y(n) ;
% Start with big mu for speeding the convergence then slow down to reach the correct weights
if n 20
mu=0.32;
else
mu=0.15;
end
w = w + mu * u * e(n) ;
end
%check of results
for n = N+1 : totallength
u = inp(n:-1:n-sysorder+1) ;
y(n) = w' * u ;
e(n) = d(n) - y(n) ;
end
hold on
plot(d)
plot(y,'r');
title('System output') ;
xlabel('Samples')
ylabel('True and estimated output')
figure
semilogy((abs(e))) ;
title('Error curve') ;
xlabel('Samples')
ylabel('Error value')
figure
plot(h, 'k+')
hold on
plot(w, 'r*')
legend('Actual weights','Estimated weights')
title('Comparison of the actual weights and the estimated weights') ;
axis([0 6 0.05 0.35])
% RLS 算法
randn('seed', 0) ;
rand('seed', 0) ;
NoOfData = 8000 ; % Set no of data points used for training
Order = 32 ; % Set the adaptive filter order
Lambda = 0.98 ; % Set the forgetting factor
Delta = 0.001 ; % R initialized to Delta*I
x = randn(NoOfData, 1) ;% Input assumed to be white
h = rand(Order, 1) ; % System picked randomly
d = filter(h, 1, x) ; % Generate output (desired signal)
% Initialize RLS
P = Delta * eye ( Order, Order ) ;
w = zeros ( Order, 1 ) ;
% RLS Adaptation
for n = Order : NoOfData ;
u = x(n:-1:n-Order+1) ;
pi_ = u' * P ;
k = Lambda + pi_ * u ;
K = pi_'/k;
e(n) = d(n) - w' * u ;
w = w + K * e(n) ;
PPrime = K * pi_ ;
P = ( P - PPrime ) / Lambda ;
w_err(n) = norm(h - w) ;
end ;
% Plot results
figure ;
plot(20*log10(abs(e))) ;
title('Learning Curve') ;
xlabel('Iteration Number') ;
ylabel('Output Estimation Error in dB') ;
figure ;
semilogy(w_err) ;
title('Weight Estimation Error') ;
xlabel('Iteration Number') ;
ylabel('Weight Error in dB') ;