how to create random no between 10 to 40 in C

can any one tell me how to create integer random no between 10 to 40 in C language.When i m using random() or rand() functions they r creting some
long int which is not required

Divide the random number by 31 ( 10 through 40) take the remainder and add 10.
Create an array of 31 entries, run 1,000,000 iterations and count how many times each number appears to see if you have 25000 of each number, or what the deviation is.

The below example shows a simple solution of your problem.

#include <stdio.h>
#include <stdlib.h>

int main ()
{
  /* initialize random generator */
  srand ( time(NULL) );

  /* generate random numbers */ 	
  printf ("A number between 10 and 40: %d\n", (rand() % 30 + 10) );

  return 0;
}

Best regards,
Iliyan Varshilov

Why the resule is being divided by 30 to get the result & added to 10.

How to reach the above decision.

Because a random number between 10 and 40 was requested - the original result (which would be between 0 and 30) is just shifted by adding 10.
Cheers
ZB

hi

this is not the way you should create a random number between 30 and 40.
a correct way is

#include<stdio.h>
#include<stdlib.h>
int main()
{
    printf("%d\n",30+(int)((rand()/(RAND_MAX +1.0))*11));
return 0;
}

you should not use the % operator to constract a random number from the last digits of a a random number generator. most random generators are neither contructed that their last digits are random nor are they testet against this assumption. They are constructed and tested that the leading digits are random.

rand() gives a random number bertween 0 and RAND_MAX

rand()/(RAND_MAX +1.0) gives a float random number greater or equal 0 and less then 1

(rand()/(RAND_MAX +1.0))*11 gives a random number greater or equal 0 and less then 11

(int)((rand()/(RAND_MAX +1.0))*11) gives a random number from the set {0,...,10}

finally 30+(int)((rand()/(RAND_MAX +1.0))*11) gives a random number from the set {30,...,40}

information on random generators and further links can be found at
USING THE C OR C++ rand() FUNCTION
mfg guenter