Debugging Random Function in C: Generates Only "3

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Function Random
Click For Summary

Discussion Overview

The discussion revolves around debugging a random number generation function in C, specifically addressing issues related to the initialization of the random number generator and the behavior of the random function within loops. Participants explore various aspects of random number generation, including the use of the `srand()` function and the implications of calling it multiple times.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the random function only generates a "3" and questions why it does not produce a random number.
  • Another participant suggests that the random generator needs to be initialized with `srand()` to produce varied results, mentioning the use of `/dev/urandom` or `time()` as potential initial values.
  • A participant modifies the random function to include a loop but does not clarify if this resolves the issue.
  • Concerns are raised about the behavior of the random function within a loop, where subsequent calls return the same value, prompting questions about the placement of `srand()`. It is suggested that `srand()` should only be called once in `main()`.
  • Participants discuss the implications of calling `srand(time(0))` multiple times, noting that it can lead to generating the same sequence of random numbers.
  • There is a query about using logical expressions with AND/OR statements in C, with participants confirming that such expressions are valid.
  • One participant describes an attempt to generate a random array and expresses confusion about the output, leading to a discussion about the function's return behavior and the conditions under which it terminates.
  • Another participant points out that the function is called twice, which affects the output and suggests removing the first call to clarify the results.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of initializing the random number generator with `srand()`, but there are differing views on the implications of its placement and the behavior of the random function in loops. The discussion remains unresolved regarding the best practices for random number generation in this context.

Contextual Notes

Participants express uncertainty about the behavior of the random function when called multiple times and the impact of calling `srand()` repeatedly. There are also unresolved questions about the expected output of the random array generation function.

  • #31
sandy.bridge said:
It seems that it isn't returning the ith repeat.

You should still remove the first call to random(k) in your main() function.
It obfuscates the results.
 
Technology news on Phys.org
  • #32
Thanks! I had that out before but I must have opened the wrong one. Eventually I will get this random function down pat!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
2K
Replies
11
Views
2K
Replies
22
Views
5K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K