Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.
Login
Login
OnlineGDB Q&A
Questions
Unanswered
Tags
Ask a Question
Ask a Question
How do I assign a variable to a random integer in c++?
0
votes
asked
Nov 6, 2018
by
Marshall
(
390
points)
Please
log in
or register to answer this question.
1 Answer
0
votes
answered
Nov 6, 2018
by
anonymous
selected
Nov 7, 2018
by
Marshall
Best answer
#include <time.h> int main() { int randomnumber ; int max; srand( time(NULL) ); scanf("Enter total number of students %d",&max); randomnumber=rand()% 30; printf("This is your random number\n %d",randomnumber); getchar(); return 0; }
commented
Nov 7, 2018
by
Marshall
(
390
points)
Thank You, here is what i did, run this and see if you can improve anything.
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main()
{
int i;
for(;;)
{
srand((unsigned int)time(NULL));
int x=rand()%10;
std::cout<<"I'm thinking of a number between 1 and 10"<<std::endl;
int q;
std::cout<<"Is it: ";
std::cin>>q;
if(x==q)
{
std::cout<<"Correct"<<std::endl;
}
else
{
std::cout<<"Sorry, the number was "<<x<<std::endl;
}
}
return 0;
}
commented
Nov 7, 2018
by
Marshall
(
390
points)
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main()
{
int i;
for(;;)
{
srand((unsigned int)time(NULL));
int x=rand()%10;
std::cout<<"I'm thinking of a number between 1 and 10"<<std::endl;
int q;
std::cout<<"Is it: ";
std::cin>>q;
if(x==q)
{
std::cout<<"Correct"<<std::endl;
}
else
{
std::cout<<"Sorry, the number was "<<x<<std::endl;
}
}
return 0;
}
Please
log in
or register to add a comment.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...