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

Archived

This topic is now archived and is closed to further replies.

Jeff D

Random numbers in C++

11 posts in this topic

I would like to know how to make random numbers in C++. Im in my 2nd week in C++ and im almost done my first book in it. Just the book never shown me this. Thx, Jeff Desrosiers
0

Share this post


Link to post
Share on other sites
C/C++ does not have have a built in random number generator, as in it isn't part of the language. You can however use a precreated random number generator function that should be included with your compiler.

Try this in your source code:
  
#include <stdlib.h>

You can "seed" the random number generator with this function:
  
void srand ( unsigned int seed );

and you can get the next random number in the random number sequence with this function:
  
int rand ( void );

Here is how you'd use these methods:
  
int index = 0;
srand( 4 );
for( index = 0; index < 10; index++)
{
cout << rand() << endl;
}

This source code will seed the random number generator and then return ten random numbers and output them to standard out.

One thing to remember about random numbers on computers is that they aren't truely random. If you were to seed the random number generator with the value seed=2 and then call rand 12 times and you were to then reseed the random number generator to seed=2 again and call rand 12 times the values you'd get from rand would be the same for both times.

These functions for random number generation work in linux/unix so I'm not sure if they works in windows. I'm pretty sure that stdlib.h is the same for both though, so you can give it a shot.

RandomTask

Edited by - RandomTask on October 10, 2001 4:28:26 PM
0

Share this post


Link to post
Share on other sites
thx so is there something liek Qbasic''s RANDOMIZE TIMER?
can you use the time to seed the generator?



Thx,

Jeff Desrosiers
0

Share this post


Link to post
Share on other sites
You most definitely can seed the random number generator with the time. All operating systems should have some sort of function to return the system time. If you are on a linux/unix machine type "man 2 time" or "man time" on a command line to get information. If you are on a windows box do a help search for "time".

RandomTask
0

Share this post


Link to post
Share on other sites
yes, the Windows and Linux rand() functions are identical.

Windows seeding with time:

srand(time());


The time() function is declared in time.h and returns the system time in seconds. I believe it''s the same under Linux (rusty; haven''t programmed Linux in a while).
0

Share this post


Link to post
Share on other sites
Thx a lot guys Im learning slowly but smoothly I just got inheritence down to par now

Ok this is nice now for a little harder question Ive been noticing that it makes big numbers now lets say I wanted it to make a random number from 1 to 100 I know it can be done.
Kinda like Qbasics num = INT(RND * 100) + 1

if you can answer this I will be most grateful

thx,
Jeff Desrosiers

Edited by - Jeff D on October 10, 2001 10:37:29 PM
0

Share this post


Link to post
Share on other sites
This is how you do it (there are more advanced methods, but this is sufficient for your purposes I believe).

// Create your variable and intialize it to 0
int randomNumber = 0;

// Seed random nuber generator so you don''t constantly get the
// Same sequence of random numbers each time you run the program
srand(time(NULL));

// rand % 100 limits it from 0 to 99
// Add one to get 1 to 100
randomNumber = (rand() % 100) + 1;

Pretty neat, eh?
Good luck and have fun learning how to program in C++!
0

Share this post


Link to post
Share on other sites