problem with rand ()

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()
{
int x =rand(%9)+1;
cout << x ;
return 0; 
}

it shows always the same NO. each time i run the PROG.
can i have help as fast as possible
Yep. You forgot a little something that will permit the rand()s to return random numbers.
http://cplusplus.com/reference/clibrary/cstdlib/srand/

Have fun!

-Albatross

P.S: Check line 5.
whats wrong with line 5
i cant see any problem
If you compiled it, you'd know. :P You need to move the %9 over to the right a bit...

-Albatross
ok that's right.
can u give me the correct code.
thx :D
What? Is it that hard to just make the small change yourself...?
In your other post you wanted to be an expert. You can't be too dependent to others. remember that % takes left and right operand like so.. 5%3=2, left is 5, right is 3.
noooo i mean write a code doesnt print the same number each time i run it
You need to put something between lines 4 and 5. See the link I gave for a hint as to what it is.

I'm in a very helpful mood today... >_>

Oh, by the way...
forum::members.user("blackcoder41").last_post_in_thread(33723)+=std::numeric_limits<forum::user_post_value>::max();
If you want to be an expert, learn to do things yourself. We'll help you as much as we can and are willing to, but for you to get better, you need to be able to make us superfluous (did that sound wrong?). :)

-Albatross
Last edited on
Albatross wrote:
forum::members.user("blackcoder41").last_post_in_thread(33723)+=std::numeric_limits<forum::user_post_value>::max();
I'm in the mood too ^^
thnx for help me
---------
the link you give me have example for c.
(i want to learn cpp)
---------
@ end i want to say thnx for standing with me
hehe.... come on people... it want harm him if you tell him what just he have to do... rite.... dd you find out the answer Black cpp...?

time to time you will have to use C codes inside C++, dont worry... C and C++ are sisters... hehe...

http://pubs.opengroup.org/onlinepubs/009695399/nfindex.html
is also a good place to look at C things...

rand()
http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html
#include <iostream>
using namespace std;
int main()
{
srand(time(0));
int x =1+rand()%9;
cout << x ;
return 0;
}
Topic archived. No new replies allowed.