Troubleshooting Random Value Issues in C Code - Solutions and Explanation

  • Thread starter Nobody
  • Start date
  • Tags
    Random
In summary, the problem is that you need to add an srand() function call to your code to ensure that the random number generator is always seeded with a new random number.
  • #1
Nobody
18
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
  • #2
Please help me, Okay ?
Thank you...
 
  • #3
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:
  • #4
No it didn't work. Can you check it out again for me ?
Thanks
 
  • #5
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
 
  • #6
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.
 
  • #7
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::cout<<a[j]<<" ";
return 0;
}

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

What happens ?
Thanks.
 
  • #8
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.
 
  • #9
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;
}
 

1. What are some common causes of random value issues in C code?

Some common causes of random value issues in C code include uninitialized variables, out-of-bounds array access, pointer errors, and race conditions.

2. How can I prevent random value issues in my C code?

To prevent random value issues, it is important to carefully initialize all variables, properly handle arrays and pointers, and use synchronization mechanisms to avoid race conditions. Additionally, thorough testing and debugging can help identify and resolve any potential issues.

3. How can I troubleshoot random value issues in my C code?

The first step in troubleshooting random value issues is to carefully review the code and look for any potential causes, such as uninitialized variables or pointer errors. Debugging tools, such as a debugger or printf statements, can also be helpful in identifying the source of the issue. Additionally, using a systematic approach to testing and gradually introducing changes can help pinpoint the problem.

4. What are some techniques for fixing random value issues in C code?

Some techniques for fixing random value issues include carefully initializing variables, checking for errors in array and pointer usage, and using synchronization mechanisms to avoid race conditions. It may also be helpful to review the code and make sure all functions are behaving as expected.

5. Are there any best practices for writing C code to avoid random value issues?

Yes, there are several best practices for writing C code to avoid random value issues. These include properly initializing variables, using error checking for array and pointer usage, and using synchronization mechanisms when working with shared resources. It is also important to regularly test and debug the code to catch any potential issues early on.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
602
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
942
Back
Top