This question already has answers here:
What is the optimal algorithm for generating an unbiased random integer within a range?
(7 answers)
How to generate a random int in C?
(32 answers)
Closed 7 years ago.
I want to create some specific random, such that from 10 to 20.
rand()%20;
is create from 0 to 19
Apply some simple arithmetic
int val = min + (rand() % range);
Where range is (max - min) + 1
A random number betweeen 10-20 is just a random number between 0-10 shifted +10
Related
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 3 months ago.
#include <stdio.h>
typedef struct ElapsedTime_struct {
int decadesVal;
int yearsVal;
} ElapsedTime;
ElapsedTime ConvertToDecadesAndYears(int totalYears) {
ElapsedTime tempVal;
tempVal.decadesVal = totalYears/10;
tempVal.yearsVal = (((double) totalYears/10) - tempVal.decadesVal) * 10;
return tempVal;
}
int main(void) {
ElapsedTime elapsedYears;
int totalYears;
scanf("%d", &totalYears);
elapsedYears = ConvertToDecadesAndYears(totalYears);
printf("%d decades and %d years\n", elapsedYears.decadesVal, elapsedYears.yearsVal);
return 0;
}
my logic is that if you take number of total years( 26) and divide the integer value by 10, you will get the number of decades. int (26/10) = 2
my logic for the number of years is that if you take the double value of 26/10, you will get 2.6000
then subtracting 2.6 - 2 (number of decades), you will get the decimal value for the number of years(0.6)
i then multiplied by 10 to get a whole value (6) so together its 2 decades and 6 years.
however when i try running the code for some inputs (34 total years) i am getting 3 decades and 3 years for some reason whereas i should get 3 decades and 4 years.
i am confused as to why this is happening.
Floating point math is inexact. Values such as 2.6 and 3.3 cannot be exactly represented in binary floating point. You instead end up with a value that is either slightly larger or slightly smaller.
The latter case is what you're seeing. 3.4 is stored as roughly 3.39999999999999991. Subtracting 3 from that and multiplying by 10 gives you 3.9999999999999991, then when you convert that to an int the fractional part is truncated giving you 3.
That being said, you don't need floating point operations here. You can instead use the modulus operator % to get the remainder when dividing by 10.
tempVal.yearsVal = totalYears%10;
This question already has answers here:
Generate Float Random Values (also negative)
(8 answers)
How to generate random float number in C
(6 answers)
Closed 1 year ago.
I am learning C and I am trying to get a random float number between 10 and 20. This is what I have so far:
srand(time(NULL));
float x = (float) rand() / (float) (RAND_MAX) * 10 + 20;
printf("%f\n", x );
Whenever I run it only gives me a number that's something like so 20.103979. What am I doing wrong and how can I correct it?
This question already has answers here:
How do I get a specific range of numbers from rand()?
(19 answers)
Closed 7 years ago.
I'm trying to generate a 4 digits number using C. I think that we are able to use the 'rand' function, however it generates random numbers with random digits, not only 4. We have to generate a 4 - digits number.
Any help?
Thanks in advance!
you can use this if it's "randomness" isn't important:
1000+(rand()%9000)
Please try this one, i hope it will help you.
arc4random uses a better pseudo random number generator than random: it generates a statistically more uniform, less predictable distribution; is based on a larger range of possible numbers.
Also, ar4random doesn't need to be initialized as its seed is auto-generated.
E.g. generate numbers :1000~9999
int randomID = arc4random() % 9000 + 1000;
generate numbers :1000~2999
int randomID = arc4random() % 2000 + 1000;
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 7 years ago.
Why does output equal zero in this code?
Number 1 with weight of 2, number two with weight of 3 and number three with weight of 5. I can not understand why output = 0.
#include <stdio.h>
int main ()
{
float A ,B, C ,MEDIA=0 ;
scanf("%f%f%f",&A ,&B,&C);
MEDIA+=1/2*A + 1/3*B + 1/5*C;
printf("MEDIA = %.1f", MEDIA );
return 0;
}
MEDIA+=1/2*A + 1/3*B + 1/5*C;
Because 1/2, 1/3 and 1/5 will be evaluated as 0. Since they are integers.
Either write
1.0/2, 1.0/3 and 1.0/5 instead. So the compiler will know to treat the result as float.
Or
MEDIA+=A/2 + B/3 + C/5;
P.S.
Maybe I am wrong but if I understood correctly what you wrote at the description then I think your calculation of the weighted average is incorrect. It should be something like
(A*2 + B*3 + C*5)/10
This question already has answers here:
How to generate a random integer number from within a range
(11 answers)
Closed 8 years ago.
I would like to generate random numbers between 1 and 25 with the method rand().
But I only know how to generate random numbers this way, which includes the number zero by default:
int r = rand() % 26 /* random int between 0 and 25 */
Anyone? Thank you.
Very simple
int r = 1 + rand() % 25 /* random int between 1 and 25 */
but you should use this
int r = (int)(1.0 + 25.0 * rand() / RAND_MAX)
as mentioned in the comments, the second is the more robust way to generate random numbers see this link