/*This program uses random function to generate random number which help
to calculate a probability of a occurrence of the specific condition. */
Source Code in java (ProbGen.java)
import java.util.Random;
class ProbGen
{
public static void main(String ar[])
{
/*
no_x,no_y,no_z are variables to count the no. of x,y and
z respectevily in the output of the test.
*/
int i,no_x=0,no_y=0,no_z=0;
float rand=0;
for(i=0;i<10000;i++)
{
/*
Random.nextFloat() generates a float from 0.0 (inlcusive)
to 1.0 (exclusive).
*/
rand = new Random().nextFloat(); // [0;1)
if(rand>=0.0 && rand<0.1)
{
System.out.print("x");
no_x++;
}
else if(rand>=0.1 && rand<0.4)
{
System.out.print("y");
no_y++;
}else
{
System.out.print("z");
no_z++;
}
}
System.out.println("\nNo of x="+no_x+"\nNo of y="+no_y+"\nNo of Z="+no_z);
}
}
-------------------------------------------------------------------------------------------------------------------------
Source Code in C
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
int main(void)
{
int i,no_x=0,no_y=0,no_z=0;
float p;
clrscr();
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10000; i++)
{
p= (float)(rand() % 10)/10;
if(p>=0 && p<0.1)
{printf("x"); no_x++;}
else
if(p>=0.1 && p<=0.4)
{printf("y");no_y++;}
else
{printf("z");no_z++;}
}
printf("\nNo fo x=%d\No of y=%d\nNo of z=%d",no_x,no_y,no_z);
getch();
return 0;
}
No comments:
Post a Comment