隨機數
當咱們聽到這個詞時,第一個聽到應該是C中的rand()函數吧!我也是這樣的,下樣就一個關於這產生隨機數的小例子:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
for(i=0;i< 10;i++)
{
printf(" %d ", rand());
}
printf("\n");
return 0;
}
當我編譯運行時,他的運行結果都是同樣的,例以下面的運行結果:
FreeOS@FreeOS :~/source/test$ ./rand
846930886
1714636915
424238335
1649760492
1189641421
1350490027
1102520059
1967513926
1540383426
1303455736
FreeOS@FreeOS :~/source/test$ ./rand
846930886
1714636915
424238335
1649760492
1189641421
1350490027
1102520059
1967513926
1540383426
1303455736
咱們來看看爲何會出現這樣的結果吧!!
先看看他的定義:
表頭文件
#include<stdlib.h>
定義函數
int rand(void)
函數說明
rand()會返回一隨機數值,範圍在0至RAND_MAX 間。在調用此函數產生隨機數前,必須先利用srand()設好隨機數種子,若是未設隨機數種子,rand()在調用時會自動設隨機數種子爲1。關於隨機數種子請參考srand()。
返回值
返回0至RAND_MAX之間的隨機數值,RAND_MAX定義在stdlib.h,其值爲2147483647。
經過看他的函數說明咱們能夠了解到要想每次運行獲得的隨機數不一樣,咱們還要設置隨機數種子。在這裏咱們還要注意一下rand()返回值的範圍後面可能會用到這個知識點。
既然要設置隨機數種子,那咱們就看看如何設置吧!那咱們先看看上面提到srand()這個函數,下面就是關於srand()函數的簡要描述:
表頭文件
#include<stdlib.h>
定義函數
void srand (unsigned int seed);
函數說明
srand()用來設置rand()產生隨機數時的隨機數種子。參數seed必須是個整數,一般能夠利用geypid()或time(0)的返回值來當作seed。若是每次seed都設相同值,rand()所產生的隨機數值每次就會同樣。
大致知道怎麼用了吧!咱們仍是先看一個例子:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int i;
srand((int) time(0));
for(i=0;i< 10;i++)
{
printf(" %d ", rand());
}
printf("\n");
return 0;
}
咱們再進行編譯運行看看運行結果吧!
FreeOS@FreeOS :~/source/test$ gcc rand.c -o rand
FreeOS@FreeOS :~/source/test$ ./rand
1592998629
62065346
1618188255
474778836
1137226535
1855330403
1479439673
728663645
1630754993
1335724443
FreeOS@FreeOS :~/source/test$ ./rand
68161350
37176306
1830473139
775793046
482242175
303569985
918670071
1691631506
1020365576
1949831114
運行結果是否是不一樣了,可是咱們咱們怎麼利用這些這些隨機數呢??這麼大的數對咱們來講也沒有多大意義呀!是這樣的,咱們可對他進行範圍化分,就能夠產生咱們想要範圍內的數。那咱們就開始吧!
提早聲明下面的這段文字來本身網絡,摘自:http://blog.csdn.net/Chris_Magic/archive/2010/10/21/5957350.aspx
產生必定範圍隨機數的通用表示公式
要取得[a,b)的隨機整數,使用(rand() % (b-a))+ a;
要取得[a,b]的隨機整數,使用(rand() % (b-a+1))+ a;
要取得(a,b]的隨機整數,使用(rand() % (b-a))+ a + 1;
通用公式:a + rand() % n;其中的a是起始值,n是整數的範圍。
要取得a到b之間的隨機整數,另外一種表示:a + (int)b * rand() / (RAND_MAX + 1)。
要取得0~1之間的浮點數,可使用rand() / double(RAND_MAX)。
咱們來年這樣一個例子:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int i, nu;
srand((int) time(0));
for(i=0;i< 10;i++)
{
nu = 0 + (int)( 26.0 *rand()/(RAND_MAX + 1.0));
printf(" %d ", nu);
}
printf("\n");
return 0;
}
再看看運行結果:
FreeOS@FreeOS :~/source/test$ ./rand
2
22
3
17
19
20
6
25
18
11
FreeOS@FreeOS :~/source/test$ ./rand
0
25
9
24
25
3
17
17
23
7
FreeOS@FreeOS :~/source/test$ ./rand
15
6
7
10
7
21
7
1
21
13
這個小例子主要是生成[0,26]之間的數,一下子我會用這個小例子生成隨機字符串。可是這裏還要先說說這條語句「nu = 0 + (int)( 26.0 *rand()/(RAND_MAX + 1.0));」,有心的人會說,怎麼和上面的通用公式有點出入呀???
若是咱們使用通用公式的話,在編譯程序時加上「—Wall」的話,就會看到下面的狀況:
FreeOS@FreeOS :~/source/test$ gcc rand.c -o rand -Wall
rand.c: In function ‘main’:
rand.c:12: warning: integer overflow in expression(整數溢出在表達)
這就是爲何上面我大家注意rand()函數的返回值的範圍了。
好了,隨機數的問題就說完了,那我看看若是用隨機數生成隨機字符串吧!
下面就是一個本身小的小程序,代碼以下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, char *argv[])
{
int leng =128 ;
int i, nu ;
char buffer[256+1] ;
printf("Please Input length for the String, Default is 128, The Maxest legth is 256:");
fgets(buffer, 257, stdin);
buffer[strlen(buffer)-1] = '\0' ;
if(buffer[0] == '\0')
{
}
else
{
leng = atoi(buffer);
}
srand((int)time(0));
bzero(buffer, 257); express
for (i= 0; i< leng;i++)
{
buffer[i] = 'a' + ( 0+ (int)(26.0 *rand()/(RAND_MAX + 1.0)));
}
buffer[strlen(buffer)] = '\0';
printf("The randm String is [ %s ]\n", buffer);
return 0;
}
這 裏提醒一句,若是沒有bzero(buffer, 257);這條語句的話,可能生成的隨機字符串中就會出現亂碼的狀況,這種狀況多半是引用了未初始化的內存地址。這個小例子也只能生成小寫字母的隨機數, 要想生成其餘的字符,就看大家的發揮了。若是有什麼好的意見,喜歡你們能相互分享一下。
生成全字符隨機的字串:
cat /dev/urandom | strings -n C | head -n L
生成數字加字母的隨機字串:
cat /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | strings -n C | head -n L
其中C表示字符串的字符數,L表示要生成多少行字符。
這兩條命令能夠生成隨機字符串,你們能夠試一下,也能夠把他們放到程序中,這就看你對C的掌握了!