-
March 6th, 2009, 12:21 AM
#1
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
-
March 6th, 2009, 12:27 AM
#2
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;
-
March 6th, 2009, 12:59 AM
#3
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[]?
-
March 6th, 2009, 01:16 AM
#4
Re: Generate a Random Number based on a range of values
declare int i[10]; outside any functions
-
March 6th, 2009, 02:17 AM
#5
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.
-
March 6th, 2009, 02:29 AM
#6
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.
-
March 6th, 2009, 04:55 AM
#7
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
-
March 6th, 2009, 05:09 AM
#8
Re: Generate a Random Number based on a range of values
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.
-
March 6th, 2009, 05:13 PM
#9
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++;
}
-
March 6th, 2009, 07:08 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|