CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2003
    Posts
    20

    Generate a Random Number based on a range of values

    Hi all,

    I am trying to generate a range of random values from 0 to 1280. I tried searching on the Internet but didn't quite understand. I know rand()%10 will give me a range from 0-9.

    But how should i do it to get from 0 to 1280? At the moment, i am trying to do a guess the number program where the number is given by the user and the program will need to guess.

    So whenever say number>guessVal in guess() method then i will throw an exception of type class which does nothing. So my question is, can i go back to the head of the guess method to guess again or recursive from a catch block? Thanks

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Generate a Random Number based on a range of values

    To generate a random number between 2 numbers, you can do this

    int u = (double) rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Aug 2003
    Posts
    20

    Re: Generate a Random Number based on a range of values

    Thanks superman, it works. Do you know how i can create a global variable such as an int[]?

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Generate a Random Number based on a range of values

    declare int i[10]; outside any functions
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  5. #5
    Join Date
    Aug 2003
    Posts
    20

    Re: Generate a Random Number based on a range of values

    I managed to do it for my int array[], but when i tried to declare a int count=0 outside like the int array[] as i wish to make it a global variable, it will give me an error when i try to access it within a function like count++? At the moment, my function does nothing meaningful as i was just testing it out.

    Code:
    int pastValues[1280];
    int count=0;
    
    int aaa(int i) {
    count++;
    }
    My error message:
    `count' undeclared (first use this function)
    Last edited by shaoen01; March 6th, 2009 at 02:25 AM.

  6. #6
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Generate a Random Number based on a range of values

    What you have done is perfect.
    Make sure int count=0; comes before the function aaa.
    Or try with a different name.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  7. #7
    Join Date
    Aug 2003
    Posts
    20

    Re: Generate a Random Number based on a range of values

    You are right. I think count is a reserved word, i changed count to bb and it works. Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Generate a Random Number based on a range of values

    Quote Originally Posted by shaoen01
    I think count is a reserved word, i changed count to bb and it works.
    count is not a reserved word, though it is conceivable that there could be a name conflict.

    However, why do you want to use global variables here? With the given information, it seems that local variables with argument passing will suffice.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2004
    Posts
    102

    Re: Generate a Random Number based on a range of values

    If no other function needs access to count, you can use:

    Code:
    int pastValues[1280];
    
    int aaa(int i)
    {
    	static int count = 0; //initiallizer, only does this once your whole program
    
    	count++;
    }

  10. #10
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Generate a Random Number based on a range of values

    You've probably got a "using namespace std" somewhere that causes the name conflict with count. Someone else just had this problem fairly recently. That's just a guess on my part though. Another thing you can do is use namespaces. Don't create global variables. Define all variables within namespaces or classes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured