• Create Account

## Random function repeating

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1JonathanCCC  Members

Posted 02 April 2012 - 10:52 PM

hello, i'm a programming beginner and i'm trying to stop relying on global variables for everything and instead pass addresses through functions.

here i'm trying to get the deal function to pass back 2 random numbers, which it does fine, but the second called to deal - deal(&j1, &j2);
always results in the same 2 numbers that r1 and r2 was given even though in deal it calls rand(time(NULL)) and resets c1 and c2 to 0;

code is below, both functions are in 2 seperate source files:

#include <stdio.h>

gameplay()

{

extern char name[20];

int r1 = 0;

int r2 = 0;

int j1 = 0;

int j2 = 0;

deal(&r1, &r2);

deal(&j1, &j2);

printf("\n%s: %d %d", name, r1, r2);

printf("\n\nJonathan: %d %d", j1, j2);

}

and next source file:

#include <stdio.h>

#include <time.h>

int deal(int *card1, int *card2)

{

srand(time(NULL));

int c1;

int c2;

c1 = 0;

c2 = 0;

while (c1 == 0) { c1 = (rand() % 15); }

while (c2 == 0) { c2 = (rand() % 15); }

*card1 = c1;

*card2 = c2;

}

### #2Trienco  Members

Posted 03 April 2012 - 12:32 AM

Somehow I don't think this is the actual code. If it is, I will assume this is supposed to be plain C and is not compiled as C++ (because it shouldn't compile for a bunch of reasons).

-gameplay does not define a return type, so in C it defaults to int. However the function doesn't return anything.
-you never initialize name and yet try to print it. If you are "lucky" it prints nothing or garbage, but crashing is more likely.
-Your last printf doesn't end the line and as such doesn't flush the buffer. Don't be surprised if it doesn't show up on screen.

-You're supposed to call srand ONCE in your application. Calling it again and again can make your numbers LESS random.
-deal claims to return int, yet doesn't have any return statement.
-In C, you must define variables at the beginning of a block, not after already calling other functions (hopefully not a limitation in "new" C)
-If you want a number from 1-14, use 1 + (rand()%14) instead of all the looping
-The temporary variables are now pointless (also, you should get into the habit of initializing them _immediately_... int c1 = 0;)
f@dzhttp://festini.device-zero.de

### #3zaphod2  Members

Posted 03 April 2012 - 12:39 AM

The main cause of your problem is that time gives back seconds. So if the deals function is called multiple times in the same second it will initialize the random number generator to the same value, and gives back the same values.

But, no return type of gameplay function, and an extern internal variable? Is that possible in current C?

### #4Narf the Mouse  Members

Posted 03 April 2012 - 01:06 AM

Your problem is that each time you are seeding srand with the exact same time value. Only seed it once, with the current time value, for proper psuedo-random number generation.

### #5JonathanCCC  Members

Posted 03 April 2012 - 08:42 AM

thanks for the replies all.

Yes it is written in plain C, and sorry, there is one other source file with the main function, but all it has is the function prototypes and main immediately calls gameplay()

I have now made only the main function call srand(time(NULL)) at the very beginning and the problem of the same numbers coming out is fixed, so that was indeed the main issue. The "extern char name[20]" at the top of gameplay is because the main source file has a global variable as char name[20], I thought the extern was required to "declare" it, in any function you want to use it. That part works fine anyway. - I have also cleared up the void function return types that I forgot to add, although the dev-cpp compiler never complained!

Trienco, I did all your recommendations, but i'm not sure I understand what - Your last printf doesn't end the line and as such doesn't flush the buffer. Don't be surprised if it doesn't show up on screen - means exactly? what should I do? - here is the corrected code:

void gameplay()

{

extern char name[20];

int r1 = 0;

int r2 = 0;

int j1 = 0;

int j2 = 0;

deal(&r1, &r2);

deal(&j1, &j2);

printf("\n%s: %d %d", name, r1, r2);

printf("\n\nJonathan: %d %d", j1, j2);

}

---- AND THE DEAL SOURCE FILE ----

#include <stdio.h>

void deal(int *card1, int *card2)

{

*card1 = 1 + (rand()%14);

*card2 = 1 + (rand()%14);

}

### #6Trienco  Members

Posted 03 April 2012 - 11:05 AM

Trienco, I did all your recommendations, but i'm not sure I understand what - Your last printf doesn't end the line and as such doesn't flush the buffer. Don't be surprised if it doesn't show up on screen - means exactly? what should I do? - here is the corrected code:

When you call printf, the data is not directly going to the console, but ends up in a buffer. Only when the buffer is full or flushed will the text appear on screen. Flushing happens for example when you explicitely call fflush, close the file or with every newline character (\n). So if it works for you, chances are that either the buffer is full or it gets flushed when your program exists (or the next time another printf writes \n).
f@dzhttp://festini.device-zero.de

### #7Álvaro  Members

Posted 03 April 2012 - 11:10 AM

You should generally prefer to put the '\n' at the end of the line you are printing, not the beginning. This is the convention that most people use, and it's the reason why buffering works the way it does.

If you ever need to make sure that a line is printed although it doesn't end in a '\n', use `flush(stdout);'.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.