+ 1

why the output showing distinct numbers? little confused plz help?

why this code showing different numbers in execution the following program? #include <iostream> #include <cstdlib> using namespace std; int main() { for (int x = 1; x <= 10; x++) { cout << rand() << endl; } } output: 41 18467 6334 26500 19169 15724 11478 29358 26962 24464

27th Dec 2016, 6:45 AM
neil
neil - avatar
6 odpowiedzi
+ 2
You are telling the program to give you random numbers so yes they are different. if you want to get random num let's say from 0 to 100 then u do rand () % 100; If it did not work, try saving it inside an int variable and then use it again in the for loop.
27th Dec 2016, 8:26 AM
Kourosh Azizi
Kourosh Azizi - avatar
+ 1
No problem 👍
28th Dec 2016, 6:48 AM
Kourosh Azizi
Kourosh Azizi - avatar
+ 1
Bcoz u have used rand() function.......rand() is used to generate any random numbers.......U have applied loop till 10.....so it has printed 10 random numbers
29th Dec 2016, 8:28 AM
Mohit Kulkarni
Mohit Kulkarni - avatar
0
@kouroshazizi thanks for your help☺
28th Dec 2016, 6:26 AM
neil
neil - avatar
0
thank u for ur help @Mohitkulkarni :)
30th Dec 2016, 8:09 AM
neil
neil - avatar
- 1
hr,#((g))
29th Dec 2016, 11:02 AM
ashishkumar