c语言怎样同时产生几组不同的随机数

c语言怎样同时产生几组不同的随机数,第1张

简单一点的,r,t分别产生两组随机数,你往后加参数即可:

#include<stdlib.h>

#include<stdio.h>

int main(void){

int r=srandom((int)time(0))

int t=srandom((int)time(0))

printf("%d %d\n",r,t)

return 0

}

#include <fcntl.h>

#include <semaphore.h>

#include <pthread.h>

#include <sys/stat.h>#define LENTH 8typedef int Ttypedef struct cycle

{

T data[LENTH]

int head

int tail

sem_t *mutex

sem_t *db

int reader

} circularint init_queue(circular *queue)

int is_empty(circular queue)

int is_full(circular queue)

int insert(circular queue, const T data)

int popout(circular queue, T *data)

int queue_lenth(circular queue)

int read_queue(int tag, T *data, circular queue)

int close_queue(circular *queue)int init_queue(circular *queue)

{

queue->head = 0

queue->tail = 0

queue->mutex = sem_open("read", O_RDWR | O_CREAT, 0644, 0)

queue->db = sem_open("write", O_RDWR | O_CREAT, 0644, 0)

sem_init(queue->mutex, 0, 1)

sem_init(queue->db, 0, 1)

queue->reader = 0

return 0

}int is_empty(circular queue)

{

int ret

sem_wait(queue.mutex)

queue.reader++

if(queue.reader == 1)

sem_wait(queue.db)

sem_post(queue.mutex)

//临界

ret = (queue.head == queue.tail)

//临界区

sem_wait(queue.mutex)

queue.reader--

if(queue.reader == 0)

sem_post(queue.mutex)

return ret

}int is_full(circular queue)

{

int ret

sem_wait(queue.mutex)

queue.reader++

if(queue.reader == 1)

sem_wait(queue.db)

sem_post(queue.mutex)

//临界区

ret = (queue.head == (queue.tail + 1) % LENTH)

//临界区

sem_wait(queue.mutex)

queue.reader--

if(queue.reader == 0)

sem_post(queue.mutex)

return ret

}int insert(circular queue, const T data)

{

int ret = 0

sem_wait(queue.db)

//临界区

if(is_empty(queue))

ret = -1

queue.data[queue.tail] = data

queue.tail = (queue.tail + 1) % LENTH

sem_post(queue.db)

//临界区

return ret

}int popout(circular queue, T *data)

{

int ret = 0

sem_wait(queue.db)

//临界区

if(is_full(queue))

ret = -1

*data = queue.data[queue.head]

queue.head = (queue.head + 1) % LENTH

//临界区

sem_post(queue.db)

return ret

}int queue_lenth(circular queue)

{

int ret

sem_wait(queue.mutex)

queue.reader++

if(queue.reader == 1)

sem_wait(queue.db)

sem_post(queue.mutex)

//临界区

ret = (queue.tail - queue.head + LENTH) % LENTH

//临界区

sem_wait(queue.mutex)

queue.reader--

if(queue.reader == 0)

sem_post(queue.mutex)

return ret

}int read_queue(int tag, T *data, circular queue)

{

int ret = 0

sem_wait(queue.mutex)

queue.reader++

if(queue.reader == 1)

sem_wait(queue.db)

sem_post(queue.mutex)

//临界区

if(tag >= queue_lenth(queue))

ret = -1

*data = queue.data[queue.head + tag]

//临界区

sem_wait(queue.mutex)

queue.reader--

if(queue.reader == 0)

sem_post(queue.mutex)

return ret

}int close_queue(circular *queue)

{

sem_destroy(queue->mutex)

sem_destroy(queue->db)

return 0

}


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/264907.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-17
下一篇2023-04-17

发表评论

登录后才能评论

评论列表(0条)

    保存