M
mulligan.kyle
Hello, I am taking an introductory course in C and had an assignment recently that involved rand(). I noticed something interesting and was hoping someone might be able to explain.
My program allows the user to input an upper and lower floating point boundand my randBetween function returns a floating point number between those two bounds. My program works, however I noticed something strange when dealing with large numbers. For small numbers for my bounds, with a small range, the number printed would have several values after the decimal. However, for large numbers as my bounds with a similar range, only a few actual numbers are printed after the decimal followed by several 0's.
Here are some examples of outputs:
enter lower and upper limits separated by a comma: 20,33
Your random number is 30.922440
(the range between these bounds is 13)
enter lower and upper limits separated by a comma: 1234567,1234575
Your random number is 1234573.750000
(the range between these bounds is also 13 but less numbers are printed after the decimal)
Here is my program, please note that I understand how to seed the rand function but chose not to, however I did some testing with multiple seeds.
-Kyle
#include<stdio.h>
#include<stdlib.h>
float randomBetween(float, float);
main()
{
float userInput1,
userInput2,
randomNumber;
printf("enter lower and upper limits separated by a comma: ");
scanf("%g,%g", &userInput1,&userInput2);
randomNumber=randomBetween(userInput1, userInput2);
printf("Your random number is %f\n", randomNumber);
}
float randomBetween(float userInput1,float userInput2)
{
float random,
randomNumber,
range;
random = ((float) rand()) / (float) RAND_MAX; /* casts the rand output into float and the RAND_MAX into float so that a random floating point number between 0 and 1 is store$
range = userInput2-userInput1; /* stores the difference between the two inputs in order for a random number to be created between the two bounds */
randomNumber = (userInput1 + (random * range)); /* adds the random numberto the lower bound so that the final output is between the bounds */
return randomNumber;
}
My program allows the user to input an upper and lower floating point boundand my randBetween function returns a floating point number between those two bounds. My program works, however I noticed something strange when dealing with large numbers. For small numbers for my bounds, with a small range, the number printed would have several values after the decimal. However, for large numbers as my bounds with a similar range, only a few actual numbers are printed after the decimal followed by several 0's.
Here are some examples of outputs:
enter lower and upper limits separated by a comma: 20,33
Your random number is 30.922440
(the range between these bounds is 13)
enter lower and upper limits separated by a comma: 1234567,1234575
Your random number is 1234573.750000
(the range between these bounds is also 13 but less numbers are printed after the decimal)
Here is my program, please note that I understand how to seed the rand function but chose not to, however I did some testing with multiple seeds.
-Kyle
#include<stdio.h>
#include<stdlib.h>
float randomBetween(float, float);
main()
{
float userInput1,
userInput2,
randomNumber;
printf("enter lower and upper limits separated by a comma: ");
scanf("%g,%g", &userInput1,&userInput2);
randomNumber=randomBetween(userInput1, userInput2);
printf("Your random number is %f\n", randomNumber);
}
float randomBetween(float userInput1,float userInput2)
{
float random,
randomNumber,
range;
random = ((float) rand()) / (float) RAND_MAX; /* casts the rand output into float and the RAND_MAX into float so that a random floating point number between 0 and 1 is store$
range = userInput2-userInput1; /* stores the difference between the two inputs in order for a random number to be created between the two bounds */
randomNumber = (userInput1 + (random * range)); /* adds the random numberto the lower bound so that the final output is between the bounds */
return randomNumber;
}