Post: help with program (c++)
02-17-2012, 02:50 AM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); okay im in the process of making an over complicated guess my number game (as i normally do when books give me a challenge that is too easy) so im using random numbers and heres what i have

    
srand(time(0));
min+(rand()%max);
int my_number=rand();


the min and max values are passed into my gameloop func from main (and is entered by a user)
so as far as i know if i enter 1 as min and 10 as max,i should only get numbers from 1 to ten, but if you run this you get really random numbers in the thousands...can anyone help me to get the numbers between min and max?
(adsbygoogle = window.adsbygoogle || []).push({});
02-18-2012, 12:19 AM #2
|C++|
< ^ > < ^ >
Originally posted by Docko412 View Post
i never really used the random number thing(i did but not for anything important)
so i don't know the best way but the simplest way to get a number from
a range is by using this:
    
srand((unsigned)time(0));
int random_integer;
random_integer = (rand()%10)+1;
cout << random_integer << endl;


and you can change 10 to whatever and it should get a random number between 0 and whatever.
idk maybe you can use this code.

no, im supposed to get a number between min and max which are given by the player not me

---------- Post added at 08:19 PM ---------- Previous post was at 08:01 PM ----------

still need help here....anyone??:wtf:
lol
02-18-2012, 01:27 AM #3
Epic?
Awe-Inspiring
Originally posted by SLiiTH3R View Post
no, im supposed to get a number between min and max which are given by the player not me

---------- Post added at 08:19 PM ---------- Previous post was at 08:01 PM ----------

still need help here....anyone??:wtf:
lol


I'm a little confused by your specifications, but I think Docko412 is more or less correct.

Take this code:

    
int getRandomNumber(int min, int max) {
srand((unsigned) time(NULL));

return min + (rand() % (max - min + 1));
}


It'll return a value between min and max, inclusive.

If you want min and max to be user-specified, just get the values as input from the user, and pass those in as arguments to the function call.

Otherwise, you might want to be a little more specific with what you're looking for.
02-18-2012, 01:42 AM #4
|C++|
< ^ > < ^ >
Originally posted by Epic
I'm a little confused by your specifications, but I think Docko412 is more or less correct.

Take this code:

    
int getRandomNumber(int min, int max) {
srand((unsigned) time(NULL));

return min + (rand() % (max - min + 1));
}


It'll return a value between min and max, inclusive.

If you want min and max to be user-specified, just get the values as input from the user, and pass those in as arguments to the function call.

Otherwise, you might want to be a little more specific with what you're looking for.


no thats now what i wasnt, imagine a guess my number game where you choose the max number i can use and the minimum when you say, 1+rand()%6 your saying all numbers form 1-6, so why cant i say min+rand()%max; ????

---------- Post added at 09:42 PM ---------- Previous post was at 09:38 PM ----------

Originally posted by Epic
I'm a little confused by your specifications, but I think Docko412 is more or less correct.

Take this code:

    
int getRandomNumber(int min, int max) {
srand((unsigned) time(NULL));

return min + (rand() % (max - min + 1));
}


It'll return a value between min and max, inclusive.

If you want min and max to be user-specified, just get the values as input from the user, and pass those in as arguments to the function call.

Otherwise, you might want to be a little more specific with what you're looking for.


if your correct why doesn't this work

min + (rand() % (max - min + 1))= my_number;
02-18-2012, 01:51 AM #5
Epic?
Awe-Inspiring
Originally posted by SLiiTH3R View Post
no thats now what i wasnt, imagine a guess my number game where you choose the max number i can use and the minimum when you say, 1+rand()%6 your saying all numbers form 1-6, so why cant i say min+rand()%max; ????


Take the code I posted initially; if you were to call getRandomNumber(1, 6) it would return a single, pseudo-random number between 1 and 6 inclusive. The code I posted should work perfectly for your situation.

You cannot use min+rand()+max. Remember, You must login or register to view this content. returns a number between 0 and RAND_MAX (which is generally 32,767 or greater).
Let's just imagine a pretend situation. Let's say that rand returns 92, max is 20, and min is 10. In that case, according to your formula, the output should ultimately be 10 + 92 % 20. Modulo is done before addition, so we first do 92 % 20 (which is equal to 12). When you add 10 to it, you end up with 22. That's above your max value.
Now take my formula, the output would ultimately be 10 + (92 % (20 - 11 + 1)), which, is equal to 12, well within the boundaries of 10 and 20.



Originally posted by another user

if your correct why doesn't this work

min + (rand() % (max - min + 1))= my_number;


That is blatantly wrong. Try executing 12 = my_number. See what happens.
02-18-2012, 04:20 AM #6
|C++|
< ^ > < ^ >
Originally posted by Epic
Take the code I posted initially; if you were to call getRandomNumber(1, 6) it would return a single, pseudo-random number between 1 and 6 inclusive. The code I posted should work perfectly for your situation.

You cannot use min+rand()+max. Remember, You must login or register to view this content. returns a number between 0 and RAND_MAX (which is generally 32,767 or greater).
Let's just imagine a pretend situation. Let's say that rand returns 92, max is 20, and min is 10. In that case, according to your formula, the output should ultimately be 10 + 92 % 20. Modulo is done before addition, so we first do 92 % 20 (which is equal to 12). When you add 10 to it, you end up with 22. That's above your max value.
Now take my formula, the output would ultimately be 10 + (92 % (20 - 11 + 1)), which, is equal to 12, well within the boundaries of 10 and 20.





That is blatantly wrong. Try executing 12 = my_number. See what happens.

im confused to the limit right now, if you look at my code, in the main function i crate max and min and ask the user to enter it, then i pass min and max into my gameloop function, then in that function there is a system which couts some information and there is a point system whch needs to be revised since i changed up my plans on it a little, also you will find the main part which tries to actually set my number to any random number between min and max inclusive. I think you should understand it now

    #include <cstdlib>
#include <iostream>
#include<cmath>
#include<ctime>
using namespace std;
void gameloop(int,int);

int main(int argc, char *argv[])
{
int min;
int max;

cout<<"Welcome to Guess my number!\n";
cout<<"Choose a max number range:";
cin>>max;
cout<<"Thanks now enter the minimim number(greater than zero):";
cin>>min;

gameloop(min,max);
system("PAUSE");
return EXIT_SUCCESS;
}

void gameloop(int min,int max)
{
int a; //this num does nothing
int try_num=0;

srand(time(0));

int my_number;

min + (rand() % (max - min + 1))= my_number;// EPIC HERE IS THE PROBLEM, i need to set mynumber between max and min.


int points;

int input;

cout<<"If you decide to chicken out then hit (0)";

cout<<"Lets get started Smile, try and guess my number, the more tries it takes you the more points you loose,if you guess it on the first 3 tries and under you get a +3 bonus, if you get it on the first try then you get a +5 bonus, good luck";

cout<<"\nGold=10 points\nSilver=7 points\n bronze=3\n";

while(input!=0)
{

cout<<"Guess my number now:";

cin>>input;

if(input!=my_number){
try_num+=1;
cout<<"Try again:\n\n";
cout<<my_number;//this is only for testing if random #'s are functioning correctly
cout<<endl;
}

if(input==my_number && try_num==1)

points+=5;

else if(input==my_number && try_num>1 && try_num<=3)

points+=3;



if(input==my_number)
{
cout<<"You win this round!"<<"you have "<<points<<"points";

if(try_num==5)
{
cout<<"\n\nYOU SCORED GOLD";
}

else if(try_num==3){

cout<<"\n\nyou scored silver;";
}







}
}
}










i apologize for a lack of comments i didnt think i would need help with this or need to show it to anyone else but myself, i did highlight the problem in a comment so you can find it.

---------- Post added 02-18-2012 at 12:20 AM ---------- Previous post was 02-17-2012 at 10:04 PM ----------

Originally posted by Epic
Take the code I posted initially; if you were to call getRandomNumber(1, 6) it would return a single, pseudo-random number between 1 and 6 inclusive. The code I posted should work perfectly for your situation.

You cannot use min+rand()+max. Remember, You must login or register to view this content. returns a number between 0 and RAND_MAX (which is generally 32,767 or greater).
Let's just imagine a pretend situation. Let's say that rand returns 92, max is 20, and min is 10. In that case, according to your formula, the output should ultimately be 10 + 92 % 20. Modulo is done before addition, so we first do 92 % 20 (which is equal to 12). When you add 10 to it, you end up with 22. That's above your max value.
Now take my formula, the output would ultimately be 10 + (92 % (20 - 11 + 1)), which, is equal to 12, well within the boundaries of 10 and 20.





That is blatantly wrong. Try executing 12 = my_number. See what happens.


hey i fixed it with a different approach which is just as effective, but now im having trouble with the number of tries variable(try_num). it goes up 1 each time you get an answer wrong, can you look at it for me?
    #include <cstdlib>
#include <iostream>
#include<cmath>
#include<ctime>

using namespace std;
int randnum(int min,int max)
{
return min + (rand() % (max - min + 1));
}

void gameloop(int,int);

int main(int argc, char *argv[])
{
int min;
int max;

cout<<"Welcome to Guess my number!\n";
cout<<"Choose a max number range:";
cin>>max;
cout<<"Thanks now enter the minimim number(greater than zero):";
cin>>min;

gameloop(min,max);
system("PAUSE");
return EXIT_SUCCESS;
}

void gameloop(int min,int max)
{
int a; //this num does nothing
int try_num=0;

srand(time(NULL));

// EPIC HERE IS THE PROBLEM, i need to set mynumber between max and min.

int points;

int input;

cout<<"If you decide to chicken out then hit (0)";

cout<<"\nGold=5 points\nSilver=3\n";

while(input!=0)
{

cout<<"Guess my number now:";

cin>>input;

if(input!=randnum(min,max)){
try_num+=1;
cout<<"Try again:\n\n";
cout<<randnum(min,max);//this is only for testing if random #'s are functioning correctly
cout<<endl;
}

if(input==randnum(min,max) && try_num==1)

points+=5;

else if(input==randnum(min,max) && try_num>1 && try_num<=3)

points+=3;



if(input==randnum(min,max))
{
cout<<"You win this round!"<<"you have "<<points<<"points";

if(try_num==5)
{
cout<<"\n\nYOU SCORED GOLD";
break;
}

else if(try_num==3){

cout<<"\n\nyou scored silver;";
break;
}







}
}
}



Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo