1. ## randrange()

I want to generate a number in a given range as in the code below. However i get that i'm giving invalid operands to the modulus operator.
Code:
```int divide(){
while(TRUE) {
float rand1 = (correct > 11) ? ( rand() % 1000 + 1) : ( rand() % 100 +1);
float rand2 = (correct > 11) ? ( rand() % rand1 + 1): ( rand() % rand1 +1 );

float r_ans;
float ans;
Prob+=1;

/* if top number less than bottom number then switch numbers
rand1 = (rand1 < rand2) ? rand2 : rand1;
rand2 = (rand1 < rand2) ? rand1 : rand2;  */
r_ans = rand1 / rand2;

system("CLS");
title();

printf("To exit press any letter\n\n");
printf("Problem: %d\nCorrect: %d\nWrong: %d\nGrade: %.1f\n\n",Prob, correct, wrong, ((float)correct/Prob) * 100);
/* later add correct updating into system("title") bar */
printf("What is the Quotient. Answer as N.N Ex: 1. 0 :\n\n ");

printf("%4.1f  Dividend\n", rand1);
printf(" ----\n");
printf("%5.1f  Divisor\n_____\n", rand2);

while ( (scanf(" %f", &ans) ) ==1){
if (ans == r_ans){
correct +=1;
printf("Correct!\n");
/* if ( (correct % 2 == 0 && wrong <= 3)){
} */

system("pause");
divide();
}
else {
wrong+=1;
printf("Incorrect the answer is %.1f\n", r_ans);
/*if ( (wrong % 5 == 0 && correct < 10) ){
puts("Your Math kung-fu sucks. You need more training");
} */
system("pause");
divide();
}
}return EXIT_SUCCESS;
}
}```
isn't rand1 of type float already. What operands can i give it so i can return int the range of random 0 - rand1?

2. Code:
```        float rand1 = (correct > 11) ? ( rand() % 1000 + 1) : ( rand() % 100 +1);
float rand2 = (correct > 11) ? ( rand() % rand1 + 1): ( rand() % rand1 +1 );```
You can't use a float with the mod.

With the way you have rand1 and rand2 done you can use a plain int instead of a float

3. If you want an integer, then use an integer, don't use floating point numbers. Also, this has the identical effect, so why bother?
Code:
`float rand2 = (correct > 11) ? ( rand() % rand1 + 1): ( rand() % rand1 +1 );`
Why not just:
Code:
`rand2 = rand() % rand1 + 1;`
Because as you have it, that's all it's doing anyway.

Quzah.

4. Thanks. I see what your saying Quzah. I coded it that way intially cause it went along with my thought pattern. That would have been something to change when i refactored it. Thanks for pointing it out though.

I also wanted to use float so i can the most accurate response. Is there anyway to still do this? As it is an integer will be truncated to the nearest whole number.

5. The way you had it setup all you were gonna get were integers.

A better way to get a number is to use regular division:
Code:
```float rand1 = ((float)rand() / RAND_MAX) * somenum;
float rand2 = ((float)rand() / RAND_MAX) * rand1;```

6. Well rand() returns an int, so unless you're planning on doing some division to or with the return value, you're going to get an int anyway.

Damnit Thanatos! Stop posting a few seconds before I do. [/edit]

Quzah.

7. Originally Posted by quzah
Well rand() returns an int, so unless you're planning on doing some division to or with the return value, you're going to get an int anyway.

Damnit Thanatos! Stop posting a few seconds before I do. [/edit]

Quzah.
But thats what i'm planning to do. I want a float so i can get a more acurate response. instead of truncate the quotient to the nearest whole number. I want the user to actually specify what the quotient is to at least %.2f

again: is there anyway to get a floating point number returned from rand()?

8. again: is there anyway to get a floating point number returned from rand()?

But just in case you don't understand it:

rand() always returns an int from 0 to RAND_MAX, so we divide the result from rand() by RAND_MAX and we get a floating point number from 0 to 1, then we multiply it by someother number to increase the range from 0 to that number. After that you can add a number to move the results up or down.

So lets say we want to get a number between 50 and 100. First step is to find the difference: 100 - 50 = 50
Then we get a random number:
Code:
`float x = (float)rand() / RAND_MAX;`
Now x is a number between 0 and 1. Now we multiple it by the difference
Code:
`x *= 50;`
and we get a number between 0 and 50. Now we add the lower limit
Code:
`x += 50;`
and we got a number from 50 to 100.

9. wow. all that just to get a random number in a given range. Might as well just write a function and keep in a seperate header file.

How annoying!

Code:
```#Python
import random
x = random.randrage(0,50)
print x
34```
well, whatever, thanks for clueing me in on how random works anyway.

10. Of course you can do it in one line:
Code:
```float getfloatrand(float x, float y)
{
return ( ((float)rand()/RAND_MAX) * (y-x) + x );
}```
Where is x is the lower bound and y is the upper bound

11. Originally Posted by caroundw5h
wow. all that just to get a random number in a given range. Might as well just write a function and keep in a seperate header file.

How annoying!

Code:
```#Python
import random
x = random.randrage(0,50)
print x
34```
well, whatever, thanks for clueing me in on how random works anyway.
And how does this generate a floating point number? It doesn't. You first state you want a floating point number, but then you post Python code that you imply gives you what you want, that doesn't give you a floating point number. Please make up your mind.

Quzah.

12. Originally Posted by quzah
And how does this generate a floating point number? It doesn't. You first state you want a floating point number, but then you post Python code that you imply gives you what you want, that doesn't give you a floating point number. Please make up your mind.

Quzah.
You took all that time to come back with a sarcastic remark. Incase you've missed it, the point of the code was the simplicity of the syntax towards its end goal. You trying to help me or or taunt me.
Make up your mind. I don't have time for the heavy sarcasm on this board from you or anyone else.

let me typecast it to appease you:
Code:
``` import random
x = float(random.randrange(1,23))```

13. If python is anything like C your typecasting didn't do squat.

14. Originally Posted by Thantos
If python is anything like C your typecasting didn't do squat.
Good thing its not.

For something complety different than C. Check This out. If you haven't tried python thantos your in for a treat. Python is an unbelievable language. It doesn't get in your way of programming. It just allows you to do what you need to do. Can't believe your not on the bandwagon. It's where programming is/should be headed. C/C++ are just languages. They should be designed to get things done, not complicate things. Python allows that. You should check this article out and look at python in action: Here is a simple example:
1/13/2004

So everyone knows I'm a big python fan.

Forgetting the fact that it was the first language i learnt - it is still an amazing, productive and effiecient language.

I thought it was funny because of the example they placed and how languages aren't really user friendly.
They gave the example of creating three arryays and adding them togather.
Code:
```/* Add arrays A and B and put the result in C. */
int A[10], B[10], C[10];
// possible solution
int i;
for (i=0; i<=9; i++){
C[i]=A[i]+B[i];
}```
Obviously this won't work - the aritcle says. If you code in C/C++ or even Java. Obvioulsy he never heard of Python.
#The magic code:

Code:
```
a = ["this", "is" , "an", 88,22,99]
b = ["example", "of", 56,334, 0.3, "python's powers"]
c = a + b
c
['this', 'is', 'an', 88, 22, 99, 'example', 'of', 56, 334, 0.29999999999999999, "python's powers"]```
Python takes abstraction to another level.

Again thanks for your help in regards to the rand question. A little more thought to get what i wanted that is why i was annoyed. I know you have a lot of respect for the senior members on the board so i'm gonna leave it at that and not read into your last post. Try python if you haven't. Trust me you'll be hooked.

15. I rather hang myself a few times with C/C++ but know exactly what is going on than use a language that hides things from me.

Been programming in PHP the last few weaks and while I like it for webdesign I could not see myself using it for low level programming.

I don't like the easy way, I like the harder to use but gives you more control way.

But thats just me.