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
The discussion focuses on issues with generating random numbers in C using the `rand()` function. The primary problem identified is the failure to initialize the random number generator with `srand()`, which leads to repeated outputs of the same number. It is emphasized that `srand()` should only be called once in the `main()` function to ensure varied results across multiple calls to `rand()`. Additionally, there are questions about using logical expressions in C, which are confirmed to be valid. The conversation also touches on improving a function to return the index of the first repeated random number from an array, with suggestions to simplify the approach by avoiding unnecessary arrays.
  • #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
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
11
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
22
Views
5K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K