0
Getting distinct random numbers.
Hello people, I am working on a program which generates two distinct random numbers and then adds up to the screen. I was able to get the basics but the problem is, both the random numbers are same. Like if num1=10 then num2=10. I want two different numbers. Try running the code below. Help is highly appreciated. #include<cstdlib> #include<ctime> #include <iostream> using namespace std; int main() { srand(time(NULL)); int card1=rand()%10+1; srand(time(NULL)); int card2=rand()%10+1; cout<< "Your cards are"<<" " << card1<<" " << "and"<< " " << card2; int sum= card1 + card2; cout<< endl<< "Total is:"<< " " <<sum; return 0; }
8 Antworten
+ 7
srand is a seed for random number generator
meaning, it takes a paramater and generates numbers with some computations on it
the reason we often pass time(NULL) to srand is because it will guarantee a unique number (the current time in seconds since 00:00 Jan 1st, 1970 UTC)
for example, if you use srand(42);
then each time you start the program you will get the same value on the first call of rand()
but if we pass the current time: (time(NULL)) we will most likely generate a distict number
now, what you did is calling srand(time(NULL)) twice, which cause the program to use the SAME time(NULL) value, as a second have not likely to pass since the first call
TL;DR
the time(NULL) paramater in the call srand(time(NULL)) produced the SAME number in both calls, causing a usage in the same value as the seed for srand
+ 6
1) only need to use srand once so you can remove the one above int card2=....
2) add a do while loop to generate the 2nd number with a condition to keep running if the numbers are equal
int num2;
do{
card2=rand()%10+1;
}while(card1==card2);
+ 4
just put srand(time(NULL)) one time like this
#include<cstdlib>
#include<ctime>
#include <iostream>
using namespace std;
int main()
{
srand(time(NULL));
int card1=rand()%10+1;
int card2=rand()%10+1;
cout<< "Your cards are"<<" " << card1<<" " << "and"<< " " << card2;
int sum= card1 + card2;
cout<< endl<< "Total is:"<< " " <<sum;
return 0;
}
+ 3
only one srand(time(NULL))
again, you have two of them in your program
+ 2
rand is a pseudo-random function. It produces a sequence of random numbers. The problem is that the sequence is always the same. If you run the program twice the result will be the same. That is the reason to use srand, to produce different sequences. srand needs a "feed" number. For the same number you will get the same sequence. That is the reason to use time(NULL) to feed srand, so the feed number depends on time. The problem is that the 2 calls to srand are so close in time that the time(NULL) will produce the same number and so it initialize the rand to the same sequence. The time function returns the number of seconds since Jan 1, 1970 UTC. And the 2 calls are probably less than 1 microsecond one from the other.
So in your code you were initializing the random sequence twice with the same feed number. so Card1 depends on the first number of the sequence, and Card2 depends again on the first number of the same sequence. That is why they are equals.
If you call srand only once you get Card1 depending on the first number of the sequence and Card2 on the second number. (numbers of the sequence are supposed to be random)
+ 1
You guys are awesome. Thank you!!
0
Can you please provide a logical explanation for the difference between what I did and what you suggested. What happens when we use the srand() twice??
0
Hello,
I am not being able to get out of my do..while loop. Can someone please help? When the sum is more than 22, I want the program to break out of loop and prompt "Busted!". Thanks is advance.
#include<cstdlib>
#include<ctime>
#include <iostream>
using namespace std;
int main()
{
int sum;
char tryagain;
cout<< ">" << " "<< "In the card game named 'Blackjack' players get two cards to start with, and then they are asked whether or not they want more cards. Players can continue to take as many cards as they like. Their goal is to get as close as possible to a total of 21 without going over. Face cards have a value of 10. Dealer has 18." << endl << endl;
srand(time(NULL));
int card1=rand()%10+1;
int card2=rand()%10+1;
cout<< endl<< ">" << " " << "Your cards are"<<" " << card1<<" " << "and"<< " " << card2;
sum = card1 + card2;
cout << endl << ">" << " "<< "Total:"<< " " <<sum;
cout << endl<< endl<< ">" << " "<< "Do you wish to pick another card? (y/n)";
cin>> tryagain;
while(tryagain == 'y' ){
do{
srand(time(NULL));
int newcard = rand()%10+1;
cout << ">" << " " << "New card:"<< " "<< newcard;
sum+= newcard;
cout <<endl << ">" << " " << "Total:" << sum<< endl;
if(sum > 21){
break;
}
cout << endl <<">" << " " <<"Do you want to pick another card? (y/n)";
cin>> tryagain;
}while(tryagain == 'y');
}
if (sum == 18){
cout<< "It was a draw!";
}
if(sum < 18){
cout<< "You lose!";
}
if(sum > 18 && sum < 21){
cout<< "You win!";
}
if(sum > 21){
cout<< "Busted!";
}
return 0;
}