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

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

Program is not generating random number and I am not sure why

问题

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #define ERROR_CREATE 1
  8. #define ERROR_JOIN 2
  9. // 创建要作为线程执行的函数
  10. void *thread(void *ptr)
  11. {
  12. uintptr_t type = (uintptr_t) ptr; // 线程号
  13. srand(time(NULL) + getpid());
  14. int wait = rand() % 10; // 随机生成0到10之间的数
  15. sleep(wait); // 休眠若干秒
  16. printf("线程 - %ld 等待 %d 秒\n", type, wait);
  17. return ptr; // 返回线程号
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. if (argc != 2)
  22. {
  23. fprintf(stderr, "命令行参数错误");
  24. }
  25. int num_threads = atoi(argv[1]);
  26. pthread_t threads[num_threads]; // 线程类型数组
  27. for (long i = 1; i <= num_threads; i++)
  28. {
  29. if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
  30. {
  31. // 如果创建线程出错
  32. fprintf(stderr, "错误: 无法创建线程");
  33. return ERROR_CREATE;
  34. }
  35. }
  36. // 终止每个分配的线程
  37. for (int i = 1; i <= num_threads; i++)
  38. {
  39. if (pthread_join(threads[i], NULL) != 0)
  40. {
  41. // 如果终止每个线程出错
  42. fprintf(stderr, "错误: 无法终止线程");
  43. return ERROR_JOIN;
  44. }
  45. }
  46. return 0;
  47. }

设置rand函数的种子后,我仍然得到相同的输出数字。我理解硬件速度很快,因此得到的答案与种子rand函数的速度快,因此得到的答案与时钟速度相同。有没有人知道如何从rand函数中获得更多的变化?

英文:
  1. #include&lt;pthread.h&gt;
  2. #include&lt;stdio.h&gt;
  3. #include&lt;stdint.h&gt;
  4. #include&lt;stdlib.h&gt;
  5. #include&lt;time.h&gt;
  6. #include&lt;unistd.h&gt;
  7. #define ERROR_CREATE 1
  8. #define ERROR_JOIN 2
  9. // create the function to be executed as a thread
  10. void *thread(void *ptr)
  11. {
  12. uintptr_t type = (uintptr_t) ptr; // thread number
  13. srand(time(NULL) + getpid());
  14. int wait = rand() % 10; // randomizes numbers from 0 to 10
  15. sleep(wait); // waits in time intervals of seconds
  16. printf(&quot;Thread - %ld waiting for %d seconds\n&quot;,type, wait);
  17. return ptr; // returns the thread number
  18. }
  19. int main(int argc, char **argv) {
  20. if (argc != 2) {
  21. fprintf(stderr, &quot;Error with command line arguments&quot;);
  22. }
  23. int num_threads = atoi(argv[1]);
  24. pthread_t threads[num_threads]; // array of thread types
  25. for (long i = 1; i &lt;= num_threads; i++) {
  26. if (pthread_create(&amp;threads[i], NULL, thread, (void *)i) != 0)
  27. // if there&#39;s an error creating thread
  28. {
  29. fprintf(stderr,&quot;Error: could not create thread&quot;);
  30. return ERROR_CREATE;
  31. }
  32. }
  33. // terminate each thread assigned
  34. for (int i = 1; i &lt;= num_threads; i++) {
  35. if (pthread_join(threads[i], NULL) != 0)
  36. // if there&#39;s an error ending each thread
  37. {
  38. fprintf(stderr, &quot;Error: could not terminate thread&quot;);
  39. return ERROR_JOIN;
  40. }
  41. }
  42. return 0;
  43. }

> 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 在两个循环中超出了边界。

  1. #include <pthread.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #define ERROR_CREATE 1
  8. #define ERROR_JOIN 2
  9. // 创建要作为线程执行的函数
  10. void *thread(void *ptr) {
  11. uintptr_t type = (uintptr_t) ptr; // 线程号
  12. int wait = rand() % 10; // 随机生成0到10的数字
  13. sleep(wait); // 以秒为单位的时间间隔等待
  14. printf("线程 - %ld 等待 %d 秒\n", type, wait);
  15. return ptr; // 返回线程号
  16. }
  17. int main(int argc, char **argv) {
  18. if (argc != 2) {
  19. fprintf(stderr, "命令行参数错误\n");
  20. return 1;
  21. }
  22. srand(time(NULL));
  23. int num_threads = atoi(argv[1]);
  24. pthread_t threads[num_threads]; // 线程类型的数组
  25. for (long i = 0; i < num_threads; i++) {
  26. if (pthread_create(&threads[i], NULL, thread, (void *)i) != 0)
  27. // 如果创建线程时出错
  28. {
  29. fprintf(stderr, "错误:无法创建线程");
  30. return ERROR_CREATE;
  31. }
  32. }
  33. // 终止分配的每个线程
  34. for (int i = 0; i < num_threads; i++) {
  35. if (pthread_join(threads[i], NULL) != 0)
  36. // 如果结束每个线程时出错
  37. {
  38. fprintf(stderr, "错误:无法终止线程");
  39. return ERROR_JOIN;
  40. }
  41. }
  42. }

以下是一些示例运行:

  1. $ ./a.out 2
  2. 线程 - 1 等待 3
  3. 线程 - 2 等待 7
  4. $ ./a.out 2
  5. 线程 - 1 等待 3
  6. 线程 - 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.
  1. #include &lt;pthread.h&gt;
  2. #include &lt;stdint.h&gt;
  3. #include &lt;stdio.h&gt;
  4. #include &lt;stdlib.h&gt;
  5. #include &lt;time.h&gt;
  6. #include &lt;unistd.h&gt;
  7. #define ERROR_CREATE 1
  8. #define ERROR_JOIN 2
  9. // create the function to be executed as a thread
  10. void *thread(void *ptr) {
  11. uintptr_t type = (uintptr_t) ptr; // thread number
  12. int wait = rand() % 10; // randomizes numbers from 0 to 10
  13. sleep(wait); // waits in time intervals of seconds
  14. printf(&quot;Thread - %ld waiting for %d seconds\n&quot;,type, wait);
  15. return ptr; // returns the thread number
  16. }
  17. int main(int argc, char **argv) {
  18. if (argc != 2) {
  19. fprintf(stderr, &quot;Error with command line arguments\n&quot;);
  20. return 1;
  21. }
  22. srand(time(NULL));
  23. int num_threads = atoi(argv[1]);
  24. pthread_t threads[num_threads]; // array of thread types
  25. for (long i = 0; i &lt; num_threads; i++) {
  26. if (pthread_create(&amp;threads[i], NULL, thread, (void *)i) != 0)
  27. // if there&#39;s an error creating thread
  28. {
  29. fprintf(stderr,&quot;Error: could not create thread&quot;);
  30. return ERROR_CREATE;
  31. }
  32. }
  33. // terminate each thread assigned
  34. for (int i = 0; i &lt; num_threads; i++) {
  35. if (pthread_join(threads[i], NULL) != 0)
  36. // if there&#39;s an error ending each thread
  37. {
  38. fprintf(stderr, &quot;Error: could not terminate thread&quot;);
  39. return ERROR_JOIN;
  40. }
  41. }
  42. }

and here is a couple of sample runs:

  1. $ ./a.out 2
  2. Thread - 1 waiting for 3 seconds
  3. Thread - 2 waiting for 7 seconds
  4. $ ./a.out 2
  5. Thread - 1 waiting for 3 seconds
  6. 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:

确定