problem with rand ()

Dec 30, 2010 at 7:07pm
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
Dec 30, 2010 at 7:09pm
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.
Dec 30, 2010 at 7:16pm
whats wrong with line 5
i cant see any problem
Dec 30, 2010 at 7:19pm
If you compiled it, you'd know. :P You need to move the %9 over to the right a bit...

-Albatross
Dec 30, 2010 at 7:27pm
ok that's right.
can u give me the correct code.
thx :D
Dec 30, 2010 at 7:29pm
What? Is it that hard to just make the small change yourself...?
Dec 30, 2010 at 7:32pm
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.
Dec 30, 2010 at 7:35pm
noooo i mean write a code doesnt print the same number each time i run it
Dec 30, 2010 at 7:39pm
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 Dec 30, 2010 at 7:41pm
Dec 30, 2010 at 8:03pm
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 ^^
Dec 31, 2010 at 7:27am
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
Dec 31, 2010 at 7:49am
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
Dec 31, 2010 at 10:58am
#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.