• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JonathanCCC

Random function repeating

6 posts in this topic

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:

[source]#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;
}[/source]
0

Share this post


Link to post
Share on other sites
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;)
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:

[source]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);
}[/source]
0

Share this post


Link to post
Share on other sites
[quote name='JonathanCCC' timestamp='1333464156' post='4927879']
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:
[/quote]

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

Share this post


Link to post
Share on other sites
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);'.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0