英文:
Program is not generating random number and I am not sure why
问题
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define ERROR_CREATE 1
#define ERROR_JOIN 2
// 创建要作为线程执行的函数
void *thread(void *ptr)
{
uintptr_t type = (uintptr_t) ptr; // 线程号
srand(time(NULL) + getpid());
int wait = rand() % 10; // 随机生成0到10之间的数
sleep(wait); // 休眠若干秒
printf("线程 - %ld 等待 %d 秒\n", type, wait);
return ptr; // 返回线程号
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "命令行参数错误");
}
int num_threads = atoi(argv[1]);
pthread_t threads[num_threads]; // 线程类型数组
for (long i = 1; i <= num_threads; i++)
{
if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
{
// 如果创建线程出错
fprintf(stderr, "错误: 无法创建线程");
return ERROR_CREATE;
}
}
// 终止每个分配的线程
for (int i = 1; i <= num_threads; i++)
{
if (pthread_join(threads[i], NULL) != 0)
{
// 如果终止每个线程出错
fprintf(stderr, "错误: 无法终止线程");
return ERROR_JOIN;
}
}
return 0;
}
设置rand函数的种子后,我仍然得到相同的输出数字。我理解硬件速度很快,因此得到的答案与种子rand函数的速度快,因此得到的答案与时钟速度相同。有没有人知道如何从rand函数中获得更多的变化?
英文:
#include<pthread.h>
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#define ERROR_CREATE 1
#define ERROR_JOIN 2
// create the function to be executed as a thread
void *thread(void *ptr)
{
uintptr_t type = (uintptr_t) ptr; // thread number
srand(time(NULL) + getpid());
int wait = rand() % 10; // randomizes numbers from 0 to 10
sleep(wait); // waits in time intervals of seconds
printf("Thread - %ld waiting for %d seconds\n",type, wait);
return ptr; // returns the thread number
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Error with command line arguments");
}
int num_threads = atoi(argv[1]);
pthread_t threads[num_threads]; // array of thread types
for (long i = 1; i <= num_threads; i++) {
if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
// if there's an error creating thread
{
fprintf(stderr,"Error: could not create thread");
return ERROR_CREATE;
}
}
// terminate each thread assigned
for (int i = 1; i <= num_threads; i++) {
if (pthread_join(threads[i], NULL) != 0)
// if there's an error ending each thread
{
fprintf(stderr, "Error: could not terminate thread");
return ERROR_JOIN;
}
}
return 0;
}
> Seeding the rand function, I am still getting the same number outputted. I understand the hardware is fast and therefore is getting the same answer as the clock speed is faster than seeding the rand function. Does anyone know another way of getting more variety from the rand function?
答案1
得分: 2
-
如果
argv[1]
未填写,否则会发生段错误。 -
srand()
重新设置了随机序列。由于多次使用相同的值调用它,这不是您想要的。将它移到main()
中。 -
数组
threads
在两个循环中超出了边界。
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define ERROR_CREATE 1
#define ERROR_JOIN 2
// 创建要作为线程执行的函数
void *thread(void *ptr) {
uintptr_t type = (uintptr_t) ptr; // 线程号
int wait = rand() % 10; // 随机生成0到10的数字
sleep(wait); // 以秒为单位的时间间隔等待
printf("线程 - %ld 等待 %d 秒\n", type, wait);
return ptr; // 返回线程号
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "命令行参数错误\n");
return 1;
}
srand(time(NULL));
int num_threads = atoi(argv[1]);
pthread_t threads[num_threads]; // 线程类型的数组
for (long i = 0; i < num_threads; i++) {
if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
// 如果创建线程时出错
{
fprintf(stderr, "错误:无法创建线程");
return ERROR_CREATE;
}
}
// 终止分配的每个线程
for (int i = 0; i < num_threads; i++) {
if (pthread_join(threads[i], NULL) != 0)
// 如果结束每个线程时出错
{
fprintf(stderr, "错误:无法终止线程");
return ERROR_JOIN;
}
}
}
以下是一些示例运行:
$ ./a.out 2
线程 - 1 等待 3 秒
线程 - 2 等待 7 秒
$ ./a.out 2
线程 - 1 等待 3 秒
线程 - 2 等待 6 秒
英文:
- Return if
argv[1]
is not populated otherwise it segfaults. srand()
resets the sequence. As you call it multiple times with the same value this is not what you want. Moved itmain()
.- The array
threads
is accessed out of bounds in the two loops.
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define ERROR_CREATE 1
#define ERROR_JOIN 2
// create the function to be executed as a thread
void *thread(void *ptr) {
uintptr_t type = (uintptr_t) ptr; // thread number
int wait = rand() % 10; // randomizes numbers from 0 to 10
sleep(wait); // waits in time intervals of seconds
printf("Thread - %ld waiting for %d seconds\n",type, wait);
return ptr; // returns the thread number
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Error with command line arguments\n");
return 1;
}
srand(time(NULL));
int num_threads = atoi(argv[1]);
pthread_t threads[num_threads]; // array of thread types
for (long i = 0; i < num_threads; i++) {
if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
// if there's an error creating thread
{
fprintf(stderr,"Error: could not create thread");
return ERROR_CREATE;
}
}
// terminate each thread assigned
for (int i = 0; i < num_threads; i++) {
if (pthread_join(threads[i], NULL) != 0)
// if there's an error ending each thread
{
fprintf(stderr, "Error: could not terminate thread");
return ERROR_JOIN;
}
}
}
and here is a couple of sample runs:
$ ./a.out 2
Thread - 1 waiting for 3 seconds
Thread - 2 waiting for 7 seconds
$ ./a.out 2
Thread - 1 waiting for 3 seconds
Thread - 2 waiting for 6 seconds
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论