# Debugging Random Function in C: Generates Only "3

• sandy.bridge
In summary: When you call random(), it creates an array with elements that are generated randomly.The function random() creates an array with a number of elements. This number can be input by the user.The function rand() is used to generate a number between 0 and 1.
sandy.bridge
Can someone tell me why this random function is not random? I copied it exactly from my textbook, however, it only generates a "3", rather than a random number (1, 2, or 3).

PHP:
int randomMove ()//generates a random move for player 2
{

int x = rand () % 3 + 1;
return x;

}

int main() {
int play = randomMove ();
cout << play;
return 0;
}

Hi sandy.bridge!

It would generate a random value... except that you need to initialize the random generator first...
To do so, you need a call to srand() with a value that is supposedly random.
It is still quite a challenge to get an initially random value.
On linux, the special device /dev/urandom can be used to get it.
Old school is to just get the time() result and use that as an initial value, but it fails when people world wide initialize their random value in the same second.

Perfect. Thank you I_Like_Serena! I fixed it as follows,

PHP:
int randomMove ()//generates a random move for player 2
{
int x;
for (int i=1; i<=1; i++)
{
x = rand () % 3 + 1;
}
return x;
}

int main() {
srand(time(0));
int play = randomMove ();
cout << play;
return 0;
}

Good!

One more thing! Can you use expresssions with AND/OR statements in C? For example, can I go (A==1 && B==3)||(A==2 && B==1)?

sandy.bridge said:
One more thing! Can you use expresssions with AND/OR statements in C? For example, can I go (A==1 && B==3)||(A==2 && B==1)?

Of course you can. That is exactly what C/C++ is good and efficient at.
I'm not sure if I understand you holding back on it.

I like Serena said:
It would generate a random value... except that you need to initialize the random generator first...
To do so, you need a call to srand() with a value that is supposedly random.

To be pedantic...
Keep in mind, that rand() function is pseudo-random number generator.
Mathematically it represents an iterator moving over predefined sequence.
srand() is just a moving to specific point of this sequence.
It's good for debug purposes, but shouldn't be used where true entropy is needed

In regards to the random function. Why is it when I include the random function in this loop, each time it passes through the loop a new value for x is not given? It is generating an initial random number, then all of the following numbers are the same.
PHP:
int randomDraw(int number)
{	srand(time(0));
int x;
x=rand() % number + 1;

return x;

}
...
while (i < r)
{
x=randomDraw(r);
ball[i]=x;
cout << ball[i] << " ";
i = i + 1;
}

Last edited:
sandy.bridge said:
Why is it when I include the random function in this loop, each time it passes through the loop a new value for x is not given?

You haven't shown us the code of randomDraw(), so we have no idea why it doesn't work.

AlephZero said:
You haven't shown us the code of randomDraw(), so we have no idea why it doesn't work.

Whoops, that was supposed to be in there. I just added it.

sandy.bridge said:
In regards to the random function. Why is it when I include the random function in this loop, each time it passes through the loop a new value for x is not given? It is generating an initial random number, then all of the following numbers are the same.
PHP:
int randomDraw(int number)
{	srand(time(0));
int x;
x=rand() % number + 1;

return x;

}
...
while (i < r)
{
x=randomDraw(r);
ball[i]=x;
cout << ball[i] << " ";
i = i + 1;
}

What does the time(0) function do?

If it always returns the same value then you are always initializing the random number generator with the same seed which is gaur ranted to give the same sequence of random numbers that how people test their programs.

The next problem I see is that you are calling srand() each time you call randomDraw. This is wrong you only need to call srand once and then things are initialized correctly.

All I had to do was move srand(time(0)) to the body of main for it to work

sandy.bridge said:
All I had to do was move srand(time(0)) to the body of main for it to work

Yay...

jedishrfu said:
Yay...

Your sarcasm is appreciated! . . . . . .. . ...

Okay. I need a little bit of help. I am trying generate a random array with numbers 1-k, where k is inputed by the user. I have used a function to do so, and it is supposed to return the ith element in the ray such that array=array[0]. It can be compared to drawing a random colored ball from a jar, then putting it back. Than you start counting until you draw that same ball again.

When I cout the function, it doesn't seem to output what i turns out to be. Any suggestions is greatly appreciated.

PHP:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int random(int k)
{		int i = 0;
int array[100];
while(i<100){
array[i] = rand()%k;
int y = array[0];
cout << array[i] << " ";
if (i>0 && y==array[i]){return i;}
else {i++;}
}
}

int main() {
int i, j, k, l, sum = 0;

srand(time(0));
cout << "Please enter the number of colours: ";
cin >> k;
cout << "Please enter the number of trials: ";
cin >> j;

random(k);
cout << endl << random(k);

return 0;
}

I have not used all the variables I have in this code yet, but here is what I get in the console:
PHP:
Please enter the number of colours: 50
Please enter the number of trials: 50
44 1 48 37 27 5 36 27 36 3 15 11 45 35 2 37 32 23 18 19 34 15 25 26 19 41 18 6 40 37 48 10 42 35 48 7 45 28 11 41 49 0 24 27 30 45 24 11 10 35 37 11 10 26 16 28 32 38 49 49 23 28 17 9 27 10 49 34 9 31 25 17 21 48 5 45 17 12 13 27 14 24 44 16 45 27 24 18 19 44 35 34 32 45 9 1 20 0 39 11 37 47 34 36 39 16
22

Hi sandy.bridge!

Suppose you entered 5 for the number of colours, then your program should output a value that should be around 5.
Doesn't it?

Nope. This is what happens when I use 5.
PHP:
Please enter the number of colours: 5
Please enter the number of trials: 5
1 2 0 1 3 2 3
2

i should have been 3.

sandy.bridge said:
I have not used all the variables I have in this code yet, but here is what I get in the console:
PHP:
Please enter the number of colours: 50
Please enter the number of trials: 50
44 1 48 37 27 5 36 27 36 3 15 11 45 35 2 37 32 23 18 19 34 15 25 26 19 41 18 6 40 37 48 10 42 35 48 7 45 28 11 41 49 0 24 27 30 45 24 11 10 35 37 11 10 26 16 28 32 38 49 49 23 28 17 9 27 10 49 34 9 31 25 17 21 48 5 45 17 12 13 27 14 24 44 16 45 27 24 18 19 44 35 34 32 45 9 1 20 0 39 11 37 47 34 36 39 16
22

First you execute random(50) which prints 83 numbers without you doing anything with it.
It breaks of when at index 83 the number 44 is repeated.
Then you execute random(50) again with a first number of 16, and on index=22your program breaks off since it found 16 again.

So what's the problem?

sandy.bridge said:
Nope. This is what happens when I use 5.
PHP:
Please enter the number of colours: 5
Please enter the number of trials: 5
1 2 0 1 3 2 3
2

i should have been 3.

Same thing. The first time it was 3.
But then you went on, and the second time it was 2 (when the number 3 was repeated).

Wouldn't the function terminate the first time that it happens? I thought that was how the return worked.

Is there a way for me to have to array cease to build the moment that array=array[0]? I made the array have 100 items just to ensure the number would for sure be duplicated.

sandy.bridge said:
Wouldn't the function terminate the first time that it happens? I thought that was how the return worked.

It does.
The problem is the you call the function twice from your main program.
Try to delete the first call to random(k).
That should probably clear things up.

Is there a way for me to have to array cease to build the moment that array=array[0]? I made the array have 100 items just to ensure the number would for sure be duplicated.

It does.
You do have a problem though, because if the duplication does not occur within 100 tries, the function will return an uninitialized value.
Actually, you should get a compiler warning on that.

That was the issue. Should I be trying to represent the array by infinity?

sandy.bridge said:
That was the issue. Should I be trying to represent the array by infinity?

You can't. There is no such thing.
However, you don't need an array at all.
Just replace array by some variable and keep going without limit.

Right, I figured an array would be required because y=array[0]. If I change the array to variable say x, y=x, however, x changes every circulation of loop.

sandy.bridge said:
Right, I figured an array would be required because y=array[0]. If I change the array to variable say x, y=x, however, x changes every circulation of loop.

Yes it does.
Any problem with that?
Just make sure you assign to y only once.

This is what I got:

PHP:
int random(int k)
{       int i = 0, x, y;
while(i>-1){
x = rand()%k;
if (i==0){y = x;}
cout << x << " ";
if (i>0 && y==x){return i;}
else {i++;}
}
}

However, it is giving me an error saying that my function should be void.

Last edited:
sandy.bridge said:
This is what I got:

PHP:
int random(int k)
{       int i = 0, x, y;
while(i>-1){
x = rand()%k;
if (i==0){y = x;}
cout << x << " ";
if (i>0 && y==x){return i;}
else {i++;}
}
}

However, it is giving me an error saying that my function should be void.

Yeah, well your function doesn't cleanly return an int... although it should work.

PHP:
int random(int k)
{
int y = rand() % k;
cout << y << " ";
int i = 1;
for (;; i++)
{
int x = rand() % k;
cout << x << " ";
if (y == x)
{
break;
}
}
return i;
}

Last edited:
Here is what a run comes up with:

PHP:
Please enter the number of colours: 5
Please enter the number of trials: 5
4 1 2 0 1 0 0 3 0 3 2 3 2 2 4 4 2 2 2 4
4

My bad. I just edited my previous post.

It seems that it isn't returning the ith repeat.

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.

Thanks! I had that out before but I must have opened the wrong one. Eventually I will get this random function down pat!

## 1. Why is my random function only generating the number "3" in C?

This is likely due to an error in your code. Make sure you are using the correct syntax and that your function is properly seeded. Additionally, check for any potential loops or conditions that may be limiting the range of numbers that can be generated.

## 2. How can I fix my random function to generate a wider range of numbers?

First, check that your function is properly seeded and that you are using the correct syntax. Then, consider using a different algorithm or increasing the range of numbers in your function. You can also try using a different random number generator library.

## 3. Is there a way to debug my random function in C?

Yes, there are several debugging techniques you can use. One option is to print out the values generated by your function and see if there is a pattern or error. You can also use a debugger tool to step through your code and identify any issues.

## 4. Can I use a different random number generator in C?

Yes, there are multiple random number generator libraries available in C. Some popular options include the rand() function from stdlib.h and the srand() function from time.h.

## 5. How can I ensure that my random function in C is truly random?

There is no way to guarantee true randomness in a computer-generated random function. However, you can increase the randomness by using a good seed and a reliable algorithm. It is also important to avoid any patterns or biases in your code that may skew the results.

Replies
2
Views
1K
Replies
12
Views
2K
Replies
1
Views
860
Replies
1
Views
791
Replies
5
Views
2K
Replies
9
Views
2K
Replies
3
Views
1K
Replies
11
Views
2K
Replies
23
Views
2K
Replies
22
Views
4K