This question already has answers here:
Generate random numbers using C++11 random library
(6 answers)
srand() — why call it only once?
(7 answers)
Closed 3 years ago.
I am trying to use the rand() which can generate a random number. However, I found that the code keep giving the same number when every time I compile it.
Here is the code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int a = rand() % 11;
cout << a;
return 0;
}
What you can do is include the:#include<time.h> and use srand at the top of main. This will correct this issue because it will specify the seed for the generator.
// top of main
srand(static_cast<unsigned int>(time(0)));
time(0) provides you with the seconds that have passed since Jan 1, 1970. This provides a good seed.
When you have time look into the <random> header here
You have to seed it at least 1 change to get random numbers every time you run the file:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
int random = rand();
cout << "Seed = " << time(0) << endl;
cout << "Random number = " << random << endl;
return 0;
}
Related
This question already has answers here:
How to generate a random number in C++?
(14 answers)
Closed 1 year ago.
I'm making a rock paper scissors bot that randomly picks rock, paper or scissors. I have an array called rpsoptions as shown below.
#include <iostream>
#include <string>
int main()
{
std::string rpsoptions[3] = {"rock", "paper", "scissors"};
std::cout << ( RANDOM ITEM FROM ARRAY) << std::endl;
}
How do I select a random element from the array?
You do this by:
Generating a random index into the array (in your case between 0 and 2); let that index be i.
Emitting rpsoptions[i].
As #ThomasSablik notes, the first step is covered by this question:
How to generate a random number in C++?
Combining the two steps, here is what you could get for your program:
#include <random>
#include <iostream>
int main()
{
std::random_device dev;
std::mt19937 randomness_generator(dev());
std::uniform_int_distribution<std::size_t> index_distribution(0,2);
std::string rpsoptions[3] = {"rock", "paper", "scissors"};
auto i = index_distribution(randomness_generator);
std::cout << rpsoptions[i] << std::endl;
}
Note that I've glossed over the issue of seeding the pseudo-random number generator; that's covered in the first answer at the link above, and would result in a bunch more code. It's important when you actually want to rely on properties of your random distribution, but not so important to illustrate the way you use the (pseudo-)randomly-generated index.
You only need to generate a random number and use it to choose the element into the array:
int main() {
srand(time(NULL));
std::string rpsoptions[3] = {"rock", "paper", "scissors"};
std::cout << rpsoptions[rand() % 3] << std::endl;
return 0;
}
where rand() % 3 generates a random integer number in [0, 2] range (number of the elements in the array you have).
This question already has answers here:
Random number generator only generating one random number
(15 answers)
srand() — why call it only once?
(7 answers)
Closed 7 years ago.
I'm working on a program that uses a function that would generate 2 random integers that I would later use to ask the user to add and figure out the sum. For the time being, I came across a problem in which it's duplicating the same number.
Here's the program thus far:
#include<iostream>
#include<ctime>
using namespace std;
/*
This function generates a random integer
between 0 and max_int
*/
int get_rand_int(int max_int){
srand(time(0));
return int(rand()) % max_int;
}
This program generates random addition problems
with integers between 0 and 100
int main(){
int num1 = get_rand_int(101);
int num2 = get_rand_int(101);
cout << num1 << endl;
cout << num2 << endl;
Please note this this is a homework problem my teacher assigned. He provided us with the skeleton and we're trying to fill in the blanks. I have a feeling the problem lies within the srand section of my function but that was part of my teacher's work so I'm not sure whether I need to touch that. Thanks in advance.
Yes. srand initializes the random sequence with the result of time. However, time changes only once per second! This means that if you call your function twice in a one-second delay, you get the same number.
One solution is to call srand just once, at the beginning of your program (in the main function, for instance).
You must seed the random generator only once in your program,
srand(time(0)); // this must happen only once, at the beginning of main()
You now seed it inside the function get_rand_int, and call the function twice in a very short amount of time. The seed ends up being the same, so you get the same sequence of numbers.
Call the srand(time(0)) in the main to set the current time only once to use random generation instantly.
int main(){
srand(time(0));
int num1 = get_rand_int(101);
int num2 = get_rand_int(101);
cout << num1 << endl;
cout << num2 << endl;
}
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Randomly shuffle C++ array (different each time)
(1 answer)
Closed 8 years ago.
Im working on a hangman game for class, i'm having trouble getting a random number.
Everytime I run the code I get the same number. Not sure what the problem is here, anything would help.
string pickWord(){
int random = rand() % 17;
string word = ::wordList[random];
cout << word << endl;
return word;
}
You have to seed random with time, otherwise it will always be the same.
take a look at this:
http://www.cplusplus.com/reference/cstdlib/srand/
your code should look like this
string pickWord(){
srand (time(NULL));
int random = rand() % 17;
string word = ::wordList[random];
cout << word << endl;
return word;
}
and also you have to add an include
#include <time.h>
This way, random will be dependant on the time, it is run, not on the compilation time.
I was wondering what the way is to get a random number with a range of 1-6, using the rand() method. This is to simulate a dice roll needed for me to find the average of 3 dice rolls so the type will be double.
This is a simple example to generate randoms between 1 to 6, I think you can figure the rest
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
std::cout << (rand() % 6 + 1) <<std::endl;
return 0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Rand generating same numbers
The following is tried when answering another question on StackOverflow:
#include <time.h>
#include <iostream>
using namespace std;
main() {
srand ( time(NULL) );
cout << (float) rand() / RAND_MAX << endl;
cout << ((float) rand()) << endl;
cout << RAND_MAX << endl;
cout << (float) rand() / RAND_MAX << endl;
}
Very strangely, the first output number is always a similar number, either on Windows 7 with cygwin or on Mac with Leopard.
The last number is a good random number from 0 to 1. If the first cout line is commented out, the first printed random number is always a similar value one.
How could that be happening?
I have stumbled upon this phenomenon myself in the past. The first call to rand() in four sequential runs of a test program gave the following output:
27592
27595
27598
27602
Notice how similar those numbers are? This is because the random number generator is initialized with the current time, and the first result is heavily influenced by that. Similar initial values for srand yield similar initial results for rand. It's as simple as that.
This similarity is irrelevant if you calculate rand() % n, but if you go with the rand() / m approach, this is a problem. For example, if you divide rand() by 100, you will get the same number 3 times in a row!
Now let's take a look at the second result of rand() in four sequential runs:
11520
22268
248
10997
This looks much better, doesn't it? A simple quick-fix is to call rand() a few times after seeding and simply ignoring the result.
int main()
{
srand(time(0));
rand(); rand(); rand();
std::cout << rand() / float(RAND_MAX) << std::endl;
}
rand() function in VS2008 returns this: return ((current_value = current_value * 214013 + 2531011) >> 16) & 0x7fff;. This current_value you set with srand. This function is such that it will return similar pseudo random numbers for similar seeds, and there is no help about it. The problem is that those bits that are the most random in first call are eaten up with >> 16 part. To workaround the problem just roll it a few times (rand(); rand();).