程序未生成随机数,我不确定原因。

huangapple go评论61阅读模式
英文:

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&lt;pthread.h&gt;
#include&lt;stdio.h&gt;
#include&lt;stdint.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;time.h&gt;
#include&lt;unistd.h&gt;
#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(&quot;Thread - %ld waiting for %d seconds\n&quot;,type, wait);
    return  ptr; // returns the thread number
}

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, &quot;Error with command line arguments&quot;);
    }

    int num_threads = atoi(argv[1]);
    pthread_t threads[num_threads]; // array of thread types

    for (long i = 1; i &lt;= num_threads; i++) {
        if (pthread_create(&amp;threads[i], NULL, thread, (void *)i) != 0)
            // if there&#39;s an error creating thread
        {
            fprintf(stderr,&quot;Error: could not create thread&quot;);
            return ERROR_CREATE;
        }
    }
        // terminate each thread assigned
    for (int i = 1; i &lt;= num_threads; i++) {
        if (pthread_join(threads[i], NULL) != 0)
            // if there&#39;s an error ending each thread
        {
            fprintf(stderr, &quot;Error: could not terminate thread&quot;);
            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

  1. 如果 argv[1] 未填写,否则会发生段错误。

  2. srand() 重新设置了随机序列。由于多次使用相同的值调用它,这不是您想要的。将它移到 main() 中。

  3. 数组 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
英文:
  1. Return if argv[1] is not populated otherwise it segfaults.
  2. srand() resets the sequence. As you call it multiple times with the same value this is not what you want. Moved it main().
  3. The array threads is accessed out of bounds in the two loops.
#include &lt;pthread.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;unistd.h&gt;
#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(&quot;Thread - %ld waiting for %d seconds\n&quot;,type, wait);
	return  ptr; // returns the thread number
}

int main(int argc, char **argv) {
	if (argc != 2) {
		fprintf(stderr, &quot;Error with command line arguments\n&quot;);
		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 &lt; num_threads; i++) {
		if (pthread_create(&amp;threads[i], NULL, thread, (void *)i) != 0)
			// if there&#39;s an error creating thread
		{
			fprintf(stderr,&quot;Error: could not create thread&quot;);
			return ERROR_CREATE;
		}
	}
	// terminate each thread assigned
	for (int i = 0; i &lt; num_threads; i++) {
		if (pthread_join(threads[i], NULL) != 0)
			// if there&#39;s an error ending each thread
		{
			fprintf(stderr, &quot;Error: could not terminate thread&quot;);
			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

huangapple
  • 本文由 发表于 2023年2月16日 05:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定