Troubleshooting Random Value Issues in C Code - Solutions and Explanation

  • Thread starter Thread starter Nobody
  • Start date Start date
  • Tags Tags
    Random
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting issues with generating random values in C code, specifically focusing on the use of the `rand()` function and its seeding with `srand()`. Participants explore the behavior of the code provided, which is intended to fill a matrix with random values of 0 and 1, but instead produces uniform outputs.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the `rand()%2` function should return 0 or 1, but the implementation seems to only yield consistent values of either 0 or 1, suggesting a potential issue with the random number generation.
  • Another participant suggests adding `srand()` to seed the random number generator, indicating that without proper seeding, the output may not vary as expected.
  • A participant points out a syntax error in the posted code, which could complicate troubleshooting efforts, as it implies the code may not have been copied correctly.
  • One user mentions that resetting the random number generator with the same seed each time the function is called will lead to the same sequence of random numbers being generated, which could explain the lack of variability in the output.
  • Another participant proposes an alternative approach to generating random numbers, suggesting the use of `srandomdev()` and modifying the output to produce floating-point numbers instead of integers.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of seeding the random number generator to achieve varied outputs. However, there is no consensus on the exact implementation details or whether the existing code can be corrected without rewriting it.

Contextual Notes

There are unresolved syntax errors and potential misunderstandings regarding the use of `srand()` and `rand()`, as well as the implications of using the same seed multiple times. The discussion includes varying levels of code correctness and clarity, which may affect the troubleshooting process.

Who May Find This Useful

This discussion may be useful for programmers encountering issues with random number generation in C, particularly those interested in understanding the effects of seeding and the behavior of the `rand()` function.

Nobody
Messages
18
Reaction score
1
Thanks for the help
I have another problem,

int foo(){
int n=rand()%2
switch(n){
case 0:
h=1;
break;
case 1;
h=0;
}
return h;
}

void mat(int a[][5]){
for(i=0;i<5;i++)
for(j=0;j<5;j++)
a[j]=foo();
}


And in main function I can only print out all 0's or all 1's. There are no random values at all ? Why no random values ?
If you can, please explain this in assembly code and in the above C code also, I'm very grateful.
Thanks a lot
 
Computer science news on Phys.org
Please help me, Okay ?
Thank you...
 
Huh? rand()%2 will return 0 and 1 and this value is stored in n. The case statement is just inverting the n variable. You can actually invert the bit in one line by doing:

n = !n

I would add an srand() in the foo function to seed the rand number generator.
 
Last edited:
No it didn't work. Can you check it out again for me ?
Thanks
 
What exactly are u looking for ?
first of all as dduardo say,
use srand()
secondly,
a call to randomize() is also needed...

Also u say that u are getting only 1's and 0's ...
if u don't know then that's what exactly ur program is programmed to do ...
note that ur foo is returning 1 and 0's only.

-- AI
 
Nobody said:
case 1;
h=0;

What you posted had a syntax error. That means that is wasn't cut and pasted directly from the source of the program that you ran. Since we're not seeing the actual code, it makes it hard to know what the problem is.
 
I code my program in another computer, not this one.

int foo(){
srand(usignged(time(NULL));
int n=rand()%2;
switch(n){
case 0:
h=1;
break;
case 1:
h=0;
}
return h;
}

void mat(int a[][5]){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a[j]=foo();
}

In my main function

int main(){
int a[3][5];
mat(a);
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
std::count<<a[j]<<" ";
return 0;
}

I can only see alll 1's, I execute again and only see all 0's.

What happens ?
Thanks.
 
Nobody said:
srand(usignged(time(NULL));
int n=rand()%2;

Each time you generate a random number you are resetting the random number generator with the same seed. The seed is the integer value of the number of seconds since the start of 1970, and won't change during the running of the program.
 
Thank you, chronon.
But does that also mean I have to rewrite the program ? Do you know how to change the program above to make that random number generator work for me ?
 
  • #10
Example code.
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int i;

  /* seed */
  srandomdev();

  /* Get 10 random numbers (either 1 or 0) */
  for(i = 0; i < 10; i++)
    printf("%d\n", (int)random()%2 );

  return 0;
}

If you want the numbers to vary between 0 and 1, you could so something like:
(changes in bold)
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int i;

  /* seed */
  srandomdev();

  /* Get 10 random numbers */
  for(i = 0; i < 10; i++)
    printf("[b]%f[/b]\n", [b](float)random()/RAND_MAX[/b] );

  return 0;
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K