By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General Discussion - Huh, 6, why 6???

OK, so I was making some program on Dev C++, and I made it about guessing the number of cookies in a cookie jar...I told it to pick a random number, and it always chooes 6!!! You know what, here's the code..( I made it really badly, I didn't feel like putting work into it :))

 

:

 

#include<iostream>
using namespace std;
int main()

{
    system("TITLE COOKIE JAR GUESSING GAME");
    system("COLOR 2");
    int icookies;
    int ijar;
    int iguess;
   
   
   
   
    srand((unsigned int)icookies);
    icookies = rand()%10+1;
    cout<<"guess how many cookies are in the jar"<<endl;
    cin>>iguess;
    if (iguess< icookies) {
              cout<<"too low"<<endl;
              }
           if (iguess == icookies){
                      cout<<"you win!"<<endl;
                      }
                      if (iguess > icookies) {
                                
                                 cout<<"too high" << endl;
                                 }
                                
                                      cin>>iguess;
                                      if (iguess< icookies) {
              cout<<"too low"<<endl;
              }
           if (iguess == icookies){
                      cout<<"you win!"<<endl;
                      }
                      if (iguess > icookies) {
                                
                                 cout<<"too high" << endl;
                                 }
                                      cin>>iguess;
                                      if (iguess< icookies) {
              cout<<"too low"<<endl;
              }
           if (iguess == icookies){
                      cout<<"you win!"<<endl;
                      }
                      if (iguess > icookies) {
                                
                                 cout<<"too high" << endl;
                                 }
                                    
                                      system("PAUSE");
                                      return(0);
                                      }
   
   



 

Around the Network

I THINK C++ has a separate thing that you need to do to randomize the random number.

Basically, if you run the program and ask for a random number, it'll keep on giving you random numbers BUT with each time you run the program you will get the same set of random numbers because it's working with the same seed that it pulls it's random numbers from. In order to make it truly random, you need to randomize the seed as well.

I could be misunderstanding your problem, or remembered a different programming language, but I'll check it out. I do know C++, so just give me a second to read through your program



Seppukuties is like LBP Lite, on crack. Play it already!

Currently wrapped up in: Half Life, Portal, and User Created Source Mods
Games I want: (Wii)Mario Kart, Okami, Bully, Conduit,  No More Heroes 2 (GC) Eternal Darkness, Killer7, (PS2) Ico, God of War1&2, Legacy of Kain: SR2&Defiance


My Prediction: Wii will be achieve 48% market share by the end of 2008, and will achieve 50% by the end of june of 09. Prediction Failed.

<- Click to see more of her

 

^ Oh OK, thanks.....My problem is that the number is always 6....I just need help with that..



 

I just did a basic search for "C++ Random Number" in google and got this srand()



The pseudo random number generator produces a sequence of numbers that
gives the appearance of being random, when in fact the sequence will
eventually repeat and is predictable.



We can seed the generator with the srand() function. This will start
the generator from a point in the sequence that is dependent on the
value we pass as an argument. If we seed the generator once with a
variable value, for instance the system time, before our first call of
rand() we can generate numbers that are random enough for simple use
(though not for serious statistical purposes).



In our earlier example the program would have generated the same number
each time we ran it because the generator would have been seeded with
the same default value each time. The following code will seed the
generator with the system time then output a single random number,
which should be different each time we run the program.   #include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
}



Seppukuties is like LBP Lite, on crack. Play it already!

Currently wrapped up in: Half Life, Portal, and User Created Source Mods
Games I want: (Wii)Mario Kart, Okami, Bully, Conduit,  No More Heroes 2 (GC) Eternal Darkness, Killer7, (PS2) Ico, God of War1&2, Legacy of Kain: SR2&Defiance


My Prediction: Wii will be achieve 48% market share by the end of 2008, and will achieve 50% by the end of june of 09. Prediction Failed.

<- Click to see more of her

 

^ Thanks a lot, I'll try it now..



 

Around the Network

Nope, the magic number is still 6....*cries*



 

supermariogalaxy said:

 

#include
using namespace std;
int main()

{
system("TITLE COOKIE JAR GUESSING GAME");
system("COLOR 2");
int icookies;
int ijar;
int iguess;




srand((unsigned int)icookies);
icookies = rand()%10+1;
cout<<"guess how many cookies are in the jar"<
cin>>iguess;
if (iguess< icookies) {
cout<<"too low"<
}
if (iguess == icookies){
cout<<"you win!"<
}
if (iguess > icookies) {

cout<<"too high" << endl;
}

cin>>iguess;
if (iguess< icookies) {
cout<<"too low"<
}
if (iguess == icookies){
cout<<"you win!"<
}
if (iguess > icookies) {

cout<<"too high" << endl;
}
cin>>iguess;
if (iguess< icookies) {
cout<<"too low"<
}
if (iguess == icookies){
cout<<"you win!"<
}
if (iguess > icookies) {

cout<<"too high" << endl;
}

system("PAUSE");
return(0);
}


 




Flow -"The important is to pwn other ppl"

^ LOL...Yeah, it is a lot of code..(57 lines to be exact :)) (Well, atleast for a number guessing game)



 

You really should make a couple of those things "else if" instead of "if" statements



Seppukuties is like LBP Lite, on crack. Play it already!

Currently wrapped up in: Half Life, Portal, and User Created Source Mods
Games I want: (Wii)Mario Kart, Okami, Bully, Conduit,  No More Heroes 2 (GC) Eternal Darkness, Killer7, (PS2) Ico, God of War1&2, Legacy of Kain: SR2&Defiance


My Prediction: Wii will be achieve 48% market share by the end of 2008, and will achieve 50% by the end of june of 09. Prediction Failed.

<- Click to see more of her

 

^ Yeah, I know, I was just being lazy :)