C pthread

 
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <stdlib.h>
#include <time.h>
    
int randint(int sec,int num) {
    srand(time(0));  // 使用当前时间作为随机数生成器的种子
    int random_number = rand() % sec;  
    return random_number;
}


//pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
void* myFunction(void* arg) {
    int param = *(int*)arg;
    
    int count = 6;
    int i;
    for(i=0;i<count;i++){
        printf(" thread %d! i = %d\n",param,i);
        sleep(randint(5, param));
    }
    
    return NULL;
}
    
//gcc pthread.c -lpthread
int main() {
    pthread_t myThread;
    int param = 42;
    
    if(pthread_create(&myThread, NULL, myFunction, ¶m) != 0) {
        printf("Failed to create thread\n");
        return 1;
    }
    sleep(3);
    int th = 43;
    pthread_t myThread2;
    if(pthread_create(&myThread2, NULL, myFunction, &th) != 0) {
        printf("Failed to create thread\n");
        return 1;
    }

    
    // 等待新线程执行结束
    pthread_join(myThread, NULL);
    pthread_join(myThread2, NULL);
    
    printf("Main thread exiting\n");
    
    return 0;
}
    

 
xt@qisan:/opt/wks/cwks/alg02$ gcc pthread.c -lpthread
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
    thread 42! i = 0
    thread 42! i = 1
    thread 42! i = 2
    thread 43! i = 0
    thread 42! i = 3
    thread 43! i = 1
    thread 43! i = 2
    thread 42! i = 4
    thread 43! i = 3
    thread 42! i = 5
    thread 43! i = 4
    thread 43! i = 5
Main thread exiting
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
    thread 42! i = 0
    thread 43! i = 0
    thread 42! i = 1
    thread 43! i = 1
    thread 43! i = 2
    thread 43! i = 3
    thread 43! i = 4
    thread 43! i = 5
    thread 42! i = 2
    thread 42! i = 3
    thread 42! i = 4
    thread 42! i = 5
Main thread exiting
    

 
一文搞定之C语言多线程
    

 

    

 

    

 


 

  

 


C随机数

rand()可以生成一个从0到RAND_MAX(通常是INT_MAX)的随机整数

 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    
int main() {
    printf("%ld\n",time(0));//time(0) 每次增加,是变化的
    srand(time(0));  // 使用当前时间作为随机数生成器的种子
    int random_number = rand();//每次运行结果不一样
    printf("Random Number: %d\n", random_number);

    srand(10);  // 使用固定的数值
    random_number = rand();//随机产生一个数,但每次运行的结果一样
    printf("Random Number: %d\n", random_number);
    return 0;
}
    

 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(0));  // 使用当前时间作为随机数生成器的种子
    int random_number = rand() % 10;  // 生成一个0到9的随机数
    printf("Random Number: %d\n", random_number);
    return 0;
}

 
需要20起,直接+20即可 

 


 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    
int main() {
    srand(time(0));  // 使用当前时间作为随机数生成器的种子
    double random_number = (double)rand() / RAND_MAX;  // 生成一个0到1的浮点数
    printf("Random Number: %f\n", random_number);
    return 0;
}

 
xt@qisan:/opt/wks/cwks/alg02$ gcc rand.c 
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
Random Number: 0.898359

 


 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    
int main() {
    srand(time(0));  // 使用当前时间作为随机数生成器的种子
    double random_number = 5 * ((double)rand() / RAND_MAX);  // 生成一个0到5的浮点数
    printf("Random Number: %f\n", random_number);
    return 0;
}

 
xt@qisan:/opt/wks/cwks/alg02$ gcc rand.c 
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
Random Number: 0.361362
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
Random Number: 0.379276
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
Random Number: 2.196123
xt@qisan:/opt/wks/cwks/alg02$ ./a.out 
Random Number: 0.057058

 

  

 


参考文章