本文目錄一覽:
- 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(“\n\n”);
printf(“%f”,max_y);
printf(“\n\n\n\n\n\n\n\n\n\n”);
printf(“\n\n\n”);
printf(“%f”,(max_y+min_y)/2);
printf(“\n\n\n\n\n\n\n\n\n\n”);
printf(“\n\n”);
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(“\n\n”);
printf(“%f”,max_y);
printf(“\n\n\n\n\n\n\n\n\n\n”);
printf(“\n\n\n”);
printf(“%f”,(max_y+min_y)/2);
printf(“\n\n\n\n\n\n\n\n\n\n”);
printf(“\n\n”);
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
99\n\n”);
for(i=0;
i10;
i++)
printf(“%d\n”,
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 file\n”);
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(“%f\t”,x[i]);
fprintf(fp,”%lf\t”,x[i]);
if(i%5==4)
{
printf(“\n”);
}
}
if(fclose(fp)==EOF)
{
printf(“Closing error\n”);
}
}
把生成的數據放入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’) ;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/301858.html