Compile time error while generating Random Number in C++
Hello Programing Gurus,
I am doing C++ practice at my own by going through either by the Internet or the MSDN library. I am developing an application in which I wanted to generate the random number in the range between 0 to 200 which should be exactly multiples of 3 and the list of number should not be more than 10.
I know this is very simple program, I am also successful in developing the logic behind it but not able to implement it clearly.
So please help me providing some focus on it.
Thanks
Re: Compile time error while generating Random Number in C++
There are two different methods of creating random number within a given range. The first one is typical of most random number generators and simply uses a call to rand() and a modulus operator to place the random numbers within a range. The second is more advanced and returns a random number which is chosen by shuffling the elements of a vector which only contains values that exist within the specified range.
Please follow the zip file for example:
Re: Compile time error while generating Random Number in C++
The ability to generate random numbers can be useful in certain kinds of programs, that need to model random events.In real life, we often generate random results by doing things like flipping a coin, rolling a dice, or shuffling a deck of cards. These events involve so many physical variables (eg. gravity, friction, air resistance, momentum, etc…) that they become almost impossible to predict or control, and produce results that are for all intents and purposes random.
A pseudo-random number generator (PRNG) is a program that takes a starting number (called a seed), and performs mathematical operations on it to transform it into some other number that appears to be unrelated to the seed. It then takes that generated number and performs the same mathematical operation on it to transform it into a new number that appears unrelated to the number it was generated from.
Code:
# #include <stdafx.h>
# #include <iostream>
# using namespace std;
#
# unsigned int PRNG()
# {
# // our initial starting seed is 2
# static unsigned int nSeed = 2;
#
# // Take the current seed and generate a new value from it
# // Due to our use of large constants and overflow, it would be
# // very hard for someone to predict what the next number is
# // going to be from the previous one.
# nSeed = (2 * nSeed + 50);
#
# // Take the seed and return a value between 0 and 200
# return nSeed % 200;
# }
#
# int main()
# {
# // Print 100 random numbers
# for (int nCount=0; nCount < 200; ++nCount)
# {
# cout << PRNG() << "\t";
#
# // If we've printed 5 numbers, start a new column
# if ((nCount+1) % 3 == 0)
# cout << endl;
# }
# }
Re: Compile time error while generating Random Number in C++
C (and by extension C++) comes with a built-in pseudo-random number generator. It is implemented as two separate functions that live in the cstdlib header:
srand() sets the initial seed value. srand() should only be called once.
rand() generates the next random number in the sequence (starting from the seed set by srand()).
Here’s a sample program using these functions:
Code:
1. #include <stdafx.h>
2. #include <iostream>
3. #include <cstdlib> // for rand() and srand()
4. using namespace std;
5.
6. int main()
7. {
8. srand(2); // set initial seed value to 2
9.
10. // Print 200 random numbers
11. for (int nCount=0; nCount < 200; ++nCount)
12. {
13. cout << rand() << "\t";
14.
15. // If we've printed numbers, start a new column
16. if ((nCount+1) % 3 == 0)
17. cout << endl;
18. }
19. }