#### Archived

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

# 2 short questions

This topic is 6828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ve got 2 questions that I''ve been wondering for a while... 1) How do I get a randomly generated number in one my apps? I need it for a simple guessing game (which is a mini-game in my other, crappy game). 2) For all of you have finished a game- how long did it take you... from the first #include to the last }, how long did it take you to completely finish your game? Programming::~Fredric(const Annoy_Ance)

##### Share on other sites
Hello I''m not that experienced but there is a rand() function that returns a random number. You need to include stdlib.h but other than that I don''t know.

##### Share on other sites
yep

for example

rand()%256 will generate a number in that range

Q2: I am still learning C++ so no games just yet

##### Share on other sites
For random numbers:

First, #include which includes the prototypes and macros for the standard PRNG (pseudo-random number generator) functions.

Second, at the start of your program, you need to seed the PRNG using srand. You need to pass it a number like:

srand(42);

but it you seed it with a fixed number, you''ll get the same sequence every time you run the program. (Note: this is a good thing for reproducing a situation, for example, when tracking a bug.) If you want different numbers each time, then the easy way is this:

#include
#include

void main() {
srand(time(0));
}

Third, use rand() to get your numbers. It returns an integer from 0 to RAND_MAX (or is it RAND_MAX-1?). Check your documentation for specifics.

Finally, prefer (rand() / 256) over (rand() % 256). In basic PRNG''s like rand(), the low-order bits tend not to be so random. Using integer divide instead of modulo will get you better results.

There are better PRNG''s out there, but this should get you started.

---- --- -- -

##### Share on other sites
Like JesusFreak said, srand() is the fastest and easisest way to do it, but you have to pass it values. Since you probably want a completly random number youll want to use the time feature in time.h.
Confused? Here''s some sample code
first you gotta seed srand():
#include
#include
void main(){
srand(time( NULL ));
}
ok now srand is seeded, now we use rand() to generate the number itself:

number = (rand() % 98) + 2;
How does this work? The modulus sign is the last interval at wich you will allow rand() to return a value, so above rand() would return a number between 0 and 98 BUT since we have a +2 at the end it will return a number between 0 and 100.
If you dont know what im talking about intervals:
0-100: 0 is the first interval, 100 is the last.
Hope this helps

P.S.
Pardon my bad grammar and spelling

##### Share on other sites
OOPS! On my above post it doesnt tell you wich include to put: those would be stdlib.h and time.h (dont forget iostream for cin and cout )

##### Share on other sites
How ((rand() % 98) + 2) can give you a 0 or a 1 ????
(rand() % 98) give you [0,97], + 2 --> [2,99]

##### Share on other sites
I am interested in random number generator that doesnt need to include any files (so I could write it in assembly language). Any ideas?

##### Share on other sites
Another pitfall, as mentioned previously on this board somewhere:

Using the modulo method for getting a range of numbers is only flat if your range is a power of two. That''s kinda a hefty statement, so let me break it down:

rand is supposed to give you a flat probability, that is every number from 0 to RAND_MAX has an equal chance of being randomly picked. RAND_MAX happens to be 0x7FFF, or 32767.

The problem exists if you pick a random number between, say, 0 and 32000. If you use the modulo method, random numbers at and above 32000 will be aliased back to the range 0 through 767. That means there will be a slightly greater chance of picking 0 - 767 than there is of picking a number from 768 - 32000. Over the range of 0 to RAND_MAX, there are two values that translate to each of the numbers 0 to 767, whereas there is only one value for each of the numbers 768 - 32000.

This isn''t an issue if your range is a multiple of two. Why? Well, if you divide the whole range from 0 to RAND_MAX into two parts, each number over your range has exactly two corresponding random values--you have a flat distribution. Divide it by two again and you''re still flat. Since the range is a multiple of two to begin with, any multiple of two from 2 to 32768 as a range will be flat.

Example: using rand () % 2 for a coin flip gives you a 50/50 chance pretty exactly. Using rand () % 6 to simulate a die roll will give some numbers a greater chance of turning up than others.

Solution? If you''re not working with a power of two, conver the range to a double and calculate it:
int range = 6; // die roll
double scale = (double) range / (RAND_MAX + 1);
int result = (int) (rand () * scale); // gives you a REAL result

##### Share on other sites
A book called Inner Loops has a chapter dedicated to generating random numbers in assembly - I''ll try to find it at the library again and post the code..

(of course I''ve said this before and forgot to post it so if you really want it keep bugging me about it... )

It also had a quicker way to clamp the values (faster than the divide associated with % )

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011511
• ### Who's Online (See full list)

There are no registered users currently online

×