Question

C++ 1) Write a random number generator that generates random integers from -10 to 10. 2)Write...

C++
1) Write a random number generator that generates random integers from -10 to 10.

2)Write a random number generator that generates random integers from 0 to 10.

Also can you explain how does it work if possible. Thank you.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main()
{
int n,x;
srand(time(NULL)); //It is used to initialize the pseudo random number by taking time as an argument
cout<<"Enter How Many Numbers"<<endl;
cin>>n;
cout<<"The Random Numbers Generated are"<<endl;
for(int i=0;i<n;i++)
{
x=rand() % 22 -10 ; // This will display Numbers from -10 to 10
cout<<x<<endl;
}

return 0;
}

2)

#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main()
{
int n,x;
srand(time(NULL)); //It is used to initialize the pseudo random number by taking time as an argument
cout<<"Enter How Many Numbers"<<endl;
cin>>n;
cout<<"The Random Numbers Generated are"<<endl;
for(int i=0;i<n;i++)
{
x=rand() % 11 ; // This will display Numbers from 0 to 10
cout<<x<<endl;
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ 1) Write a random number generator that generates random integers from -10 to 10. 2)Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT