range?

This is a discussion on range? within the C++ Programming forums, part of the General Programming Boards category; I am not getting random value within my described limit. y? is that? Code: #include<iostream> #include<conio.h> using std::cout; using std::cin; ...

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Question range?

    I am not getting random value within my described limit. y? is that?


    Code:
    #include<iostream>
    #include<conio.h>
    using std::cout;
    using std::cin;
    using std::endl;
    void genNum(void);// prototype
    int main()
    {
    genNum();// function call
    }
    void genNum()// definition
    {
    int value1, value2, result, sum;// declare alias
    
    cout<< value1 << "+" << value2 << "=\t";// prompt the user for input
    cin>> result;// read input
    value1=1+rand()%6;// generate random integer 1-10
    value2=1+rand()%6;// generate random integer 1-10
    sum=value1+value2;// calculate sum
    if(sum==result)
    {
    cout<< "Congratulations! correct";
    }
    else
    cout<< "No. Please try again";
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Call thisonce at the start of the program:
    Code:
    srand(time(NULL));
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Unhappy

    It still not works.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >value1=1+rand()%6;// generate random integer 1-10
    >value2=1+rand()%6;// generate random integer 1-10
    When the code and comment disagree, it's a good indicator that the problem lies there. You want to use mod 10 instead of mod 6 to get a range of 0-9, then add one to get 1-10:
    Code:
    value1=1+rand()%10;// generate random integer 1-10
    value2=1+rand()%10;// generate random integer 1-10
    >srand(time(NULL));
    Better to avoid seeding the generator until the program is properly debugged Magos.
    My best code is written with the delete key.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Also put the statement in readable form
    Code:
    value1=1+rand()%10;
    really says "add one to rand then take mod". The compiler will do what you really want, but it looks like a bug to the eye. Best to use parens and better order of operations:
    Code:
    value1 = (rand() % 10) + 1;
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>really says "add one to rand then take mod".
    no it doesn't... it says take the mod of a random number, then add one... It's order of operations... mod is above addition/subtraction in every programming language/mathematical function i've ever seen in my life, with the exception of when parenthesis are changing the order...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >no it doesn't... it says take the mod of a random number, then add one... [snip accurate description]
    That wasn't WaltP's point. The point was that not everyone is familiar with the intimate details of precedence and association and will typically read an expression from left to right as is generally done. I read it as "One plus a random number modulo ten", which is okay because I know that the modulo is performed first. But, for those that may not know, confusion can arise, whereas WaltP's modification is a better representation of what is actually happening when read by the average programmer, "A random number modulo ten, plus one."

    So the issue wasn't how the compiler translates the expression (either way will have the same correct result), but how the human reader translates the expression.
    My best code is written with the delete key.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by major_small
    >>really says "add one to rand then take mod".
    no it doesn't... it says take the mod of a random number, then add one... It's order of operations... mod is above addition/subtraction in every programming language/mathematical function i've ever seen in my life, with the exception of when parenthesis are changing the order...
    Please read an entire post before contradicting. Note the phrase that states
    The compiler will do what you really want, but it looks like a bug to the eye.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I read your entire post... what I really meant was that most of the people I know learned that the 'formula' is X+rand()%Y, where X is the lowest and Y is the highest number... I also know alot of people that think you can't use rand() without the modulo after it. they think it's part of the call to rand(). for example, the way I learned it (which is wrong, I know), was that you use it like this: rand()%(10^X). IE rand()%1,rand()%10,rand()%100,etc. All they told us was that it limited the length of the number (in digits) that rand outputs... later on they told us that X+rand()%Y is what I said before...

    I'm pretty sure most people know what you're talking about, but I still think that if you show that to less experienced programmers, they wouldn't get your implementation of rand() because it's not in the form they're used to...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Exclamation

    I agree with u major_small. To be precise I agree with all of you. The problem is that I am a beginner so I usually come across such problems. I wanted to generate random numbers ie, value1=1+rand()%10; and value2=1+rand()%10;

    Now I used srand(time(0)); but it didn't work out. It gives me negative numbers of kind
    -8643247+-8643247=
    The numbers I have written are just an example not the real output numbers I get. But ofcourse the sequence is correct. I am using Microsoft visual c++ 6. Now is there any problem with my compiler or I am making a terrible mistake somewhere in the code?

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by major_small
    I read your entire post... what I really meant was that most of the people I know learned that the 'formula' is X+rand()%Y, where X is the lowest and Y is the highest number...
    [snip]
    I'm pretty sure most people know what you're talking about, but I still think that if you show that to less experienced programmers, they wouldn't get your implementation of rand() because it's not in the form they're used to...
    Funny, I have the opposite experience with people learning rand(). Most people I know learn to take the modulo to get the span 0 to y, then add x to offset to the range wanted. It's all how you learned it. If you prefer the X+rand()%Y form, I still recommend
    Code:
    X + (rand() % Y)
    for readability. It's still the same thing, shows the operations in the order you like and the parens take away any and all confusion about what order the operations are done.

    Originally posted by tu_user
    I agree with u major_small. To be precise I agree with all of you. The problem is that I am a beginner so I usually come across such problems. I wanted to generate random numbers ie, value1=1+rand()%10; and value2=1+rand()%10;

    Now I used srand(time(0)); but it didn't work out. It gives me negative numbers of kind

    The numbers I have written are just an example not the real output numbers I get. But ofcourse the sequence is correct. I am using Microsoft visual c++ 6. Now is there any problem with my compiler or I am making a terrible mistake somewhere in the code?
    I would bet there's a mistake in your code. The compiler never did this to me. But since my psychic poswer aren't working today, I can't tell for sure.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006

    Re: range?

    >-8643247+-8643247=

    The genNum function still doesn't look like this, does it?
    Code:
    void genNum()// definition
    {
    int value1, value2, result, sum;// declare alias
    // display unitialized values
    cout<< value1 << "+" << value2 << "=\t";//  prompt the user for input
    cin>> result;// read input
    value1=1+rand()%6;// generate random integer 1-10
    value2=1+rand()%6;// generate random integer 1-10
    // never display what values were generated
    // hope the user is a really good guesser
    sum=value1+value2;// calculate sum
    if(sum==result)
    {
    cout<< "Congratulations! correct";
    }
    else
    cout<< "No. Please try again";
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36
    Thanku v m. It works well now.
    see the code again. Can I make it even better?

    Code:
    #include<iostream>
    #include<conio.h>
    #include<ctime>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void genNum(void);	// prototype
    
    int main()
    {	srand(time(NULL));
    	genNum();	// function call
    }
    void genNum()	// definition
    {
    	int value1, value2, result, sum;	// declare alias
    	
    	value1=1+rand()%6;	// generate random integer 1-6
    	value2=1+rand()%6;	// generate random integer 1-6
    	cout<< value1 << "+" << value2 << "=\t";	// prompt the user for input
    	cin>> result;	// read input
    	sum=value1+value2;	// calculate sum
    
    	if(sum==result)
    	{
    			cout<< "Congratulations! correct";
    	}
    	else
    			cout<< "No. Please try again";
    }

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by tu_user
    Thanku v m. It works well now.
    see the code again. Can I make it even better?
    Yes. If they guess wrong, let them try another guess. Change if(sum==result) to a loop to ask again.

    Break the genNum routine into two routines:
    1) generate the two numbers, calculate the answer
    2) input function to get and test user response
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 03:09 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 04:24 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21