#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand ( ti
ID: 3619260 • Letter: #
Question
#include <stdio.h> #include <stdlib.h> #include <time.h>int main() { srand ( time(NULL) );
int i; for(i = 0; i < 10; i++) { int random = rand() % 100 + 1;/*create random number from 1 to 100 */ printf("random value%d ",random); }
return 0; }
Right now I have this code and I want to transform this sothat it prints out two random floating point numbers between -1.0and 1.0. Please help! #include <stdio.h> #include <stdlib.h> #include <time.h>
int main() { srand ( time(NULL) );
int i; for(i = 0; i < 10; i++) { int random = rand() % 100 + 1;/*create random number from 1 to 100 */ printf("random value%d ",random); }
return 0; }
Right now I have this code and I want to transform this sothat it prints out two random floating point numbers between -1.0and 1.0. Please help!
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand ( time(NULL) );
int i=0;
while (i <= 10)
{
float random = ((rand() ) + (rand()/ 1000)) ;
float random1 = random / 10000;
if (random1 < 1 &&random1 > -1)
{
printf("random value %f ",random1); /*create random number from 0 to 1 */
i++;
}
if (random1 > 1 &&random1 < 2)
{
printf("random value %f ",random1-2); /*createrandom number from -1 to 0 */
i++;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.