# Random function in C

1. Feb 7, 2013

### 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;
}

2. Feb 7, 2013

### I like Serena

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.

3. Feb 7, 2013

### sandy.bridge

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;
}

4. Feb 7, 2013

Good!

5. Feb 7, 2013

### sandy.bridge

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)?

6. Feb 7, 2013

### I like Serena

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.

7. Feb 7, 2013

### Graniar

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

8. Mar 1, 2013

### sandy.bridge

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: Mar 1, 2013
9. Mar 1, 2013

### AlephZero

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

10. Mar 1, 2013

### sandy.bridge

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

11. Mar 1, 2013

### Staff: Mentor

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.

12. Mar 1, 2013

### sandy.bridge

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

13. Mar 1, 2013

### Staff: Mentor

Yay...

14. Mar 2, 2013

### sandy.bridge

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

15. Mar 5, 2013

### sandy.bridge

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

16. Mar 5, 2013

### I like Serena

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?

17. Mar 5, 2013

### sandy.bridge

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.

18. Mar 5, 2013

### I like Serena

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? :tongue:

19. Mar 5, 2013

### I like Serena

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).

20. Mar 5, 2013

### sandy.bridge

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.