tags:

views:

88

answers:

3
int RandInt(int a, int b){
    return (rand()%b+a);
}

I want to generate the random character values so i call it like this

char asciiValue = RandInt(65, 90);

but my output is not quite what i expect... am i doing something wrong?

--edit put in wrong values for randint call, my bad

+2  A: 

RandInt(90, 90) will return an integer between 90 and 179. The value 90 represents a 'Z' and you can have a value up from there.

What range are you tring to get?

Capital letters would be RandInt(65, 26). Is this what you expect?

Starkey
+5  A: 

It might be better if you modify your RandInt(low, high) function to return a random number between low and high, inclusive.

int RandInt(int low, int high) {
  return low + rand() % (high - low + 1);
}

This way, getting a lowercase character will be really simple:

const char c = RandInt('a', 'z');
jbernadas
+2  A: 

Rand is a random number generator with period 2 to the 32nd power to return successive pseudorandom numbers in the range from 0 to RAND_MAX.

If you are using randomize() before the rand() %b+a, then the output will be different every time you compile the code as randomize() intializes the random number generator with a random value.

And as you are passing, randint(65,90), everytime you will get a symbol as output.

What is actually done here is

1)rand()%b

Here rand() will generate a random number which is between 0 to 90 as you are passing the value of a as 90.

2)Then it will add the value of a i.e. 65 here.The value you are passing.

Then whatever the calculated value is found it will be outputed to the screen.

This will be helpful to you.

Vibhakar SInha