KarlSciberras
Programmer
The following is a small program which
generates random numbers which can be
either 1 or -1:
The output is shown below (in red) :
Ten random numbers of either -1 or 1
-1
1
1
1
-1
1
-1
-1
-1
1
Press any key to continue . . .
The program above is not mine, but it works fine.
I have been asked to analyze this code and this
is where I need some particular help.
I cannot understand the following
part of the code:
I cannot understand why (100) is passed to the
random function when only two possible outcomes
are expected. I also cannot understand the use of
>30. What I noticed however is that when >30
is removed, the outcome is less random and more
repetitive. But how was this value determined ?
generates random numbers which can be
either 1 or -1:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
randomize();
printf("Ten random numbers of either -1 or 1\n\n");
for(i=0; i<10; i++)
printf("%d\n", random(100)>30 ? -1 : 1);
system("pause");
return 0;
}
The output is shown below (in red) :
Ten random numbers of either -1 or 1
-1
1
1
1
-1
1
-1
-1
-1
1
Press any key to continue . . .
The program above is not mine, but it works fine.
I have been asked to analyze this code and this
is where I need some particular help.
I cannot understand the following
part of the code:
Code:
random(100)>30 ? -1 : 1
I cannot understand why (100) is passed to the
random function when only two possible outcomes
are expected. I also cannot understand the use of
>30. What I noticed however is that when >30
is removed, the outcome is less random and more
repetitive. But how was this value determined ?