Coin Toss – C PROGRAM

In the last few posts I have been talking a lot about generating random numbers using C programming. We performed some basic checks to see if they were truly random, etc.

Source: Wikipedia (Public Domain)

In the last post I explained the process to generate random numbers between 0 and 1.

In this post we will be using that code to simulate a coin toss.

How?

Well, we can just generate a random number and check to see if it lies within [0,0.5) or (0.5,1] .
If it lies within the range 0.5 \textless r \leq 1 , then we would call the event a Heads, else Tails.

Well, that’s it. That’s the only principle that we would be using.

Also, if you didn’t read the last few posts, then let me mention that we are using the following Pseudo Random Number generating formula:
r_{i+1}=(a \times r_i)\mathrm{mod } m
called the Linear Congruential Generator
This algorithm generates a maximum of (m-1) random numbers with the maximum value of m-1 (Try to see why is it so).
Here, r_i is the seed.
The values of a and m are carefully chosen values.
We settled upon the following values after running some statistical checks.
a=1093 ,
m=86436 ,
c=18257

So, the first part would be to write a program to generate a number b/w 0 and 1.
The code for that is given below:

CODE:

/********************************************
*********RANDOM NUMBER GENERATOR*************
***GENERATE RANDOM NUMBER BETWEEN 0 AND 1****
********************************************/
#include<stdio.h>
#include<math.h>
/**Function that generates a random number.
Parameters: 
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
**/
int rand(int r0, int a, int m, int c){
	int r1=(a*r0+c)%m;
	return r1;
}
/**Function that generates random numbers given a seed, and stores them in an array that is passed as an argument.
Parameters:
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
n: no. of random numbers to be generated
x[n]: array that will store the random numbers
**/
void randomNos(int r0, int a, int m, int c, int n, int x[n]){
	int r1=rand(r0,a,m,c);;
	int i;
	for(i=0;i<n;i++){
		x[i]=r1;
		r1=rand(r1,a,m,c);
	}
}
main(){
	int a, m, c, r0, n;
	printf("Enter the value of a:n");
	scanf("%d",&a);
	printf("Enter the value of m:n");
	scanf("%d",&m);
	printf("Enter the value of c:n");
	scanf("%d",&c);
	printf("Enter the value of r0(initial):n");
	scanf("%d",&r0);
	printf("Enter the no. of random nos. you require:n");
	scanf("%d",&n);
	int randNos[n];
	randomNos(r0, a, m, c, n, randNos);
	//Renormalize the randomnumbers so that their values are from within [0,1]
	int i;
	double randNosNew[n];
	for(i=0;i<n;i++){
		randNosNew[i]=(double)randNos[i]/(m-1);
	}
	printf("The random numbers between 0 and 1 are:n");
	for(i=0;i<n;i++){
		printf("%lf n",randNosNew[i]);
	}
}

OUPUT:

Sample Output

Now, we can add a simple check to see if the generated random numbers lie within [0,0.5) or (0.5,1].
If they lie within the range $latex 0leq r<0.5 &s=2$, then we would call the event a Tails, else Heads.

CODE:

/********************************************
*********RANDOM NUMBER GENERATOR6*************
****************COIN TOSS*********************
********************************************/
#include<stdio.h>
#include<math.h>
/**Function that generates a random number.
Parameters: 
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
**/
int rand(int r0, int a, int m, int c){
	int r1=(a*r0+c)%m;
	return r1;
}
/**Function that generates random numbers given a seed, and stores them in an array that is passed as an argument.
Parameters:
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
n: no. of random numbers to be generated
x[n]: array that will store the random numbers
**/
void randomNos(int r0, int a, int m, int c, int n, int x[n]){
	int r1=rand(r0,a,m,c);;
	int i;
	for(i=0;i<n;i++){
		x[i]=r1;
		r1=rand(r1,a,m,c);
	}
}
/**Function that results the result of a coin toss:
Parameters:
r: a random number between 0 and 1
Returns 1 for Heads and 0 for tails
**/
int coinTossSingle(double r){
	if(r>0.5){
		return 1;
	} else if(r<0.5){
		return 0;	
	}
}
/**Function that generates n coin tosses results, given a seed and other starting conditions, and stores them in an array that is passed as an argument.
Parameters:
r0: initial (first) seed
a: scale factor , so that a*r0+c give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
n: no. of coin tosses to be generated
x[n]: array that will store the random numbers
**/
void coinToss(int r0, int a, int m, int c, int n, int results[n]){
	int randNos[n];
	randomNos(r0, a, m, c, n, randNos);
	//Renormalize the random Nos. to [0 to 1]
	int i;
	double randNosNew[n];
	for(i=0;i<n;i++){
		randNosNew[i]=(double)randNos[i]/(m-1);
	}
	for(i=0;i<n;i++){
		results[i]=coinTossSingle(randNosNew[i]);	
	}
	
}
main(){
	int a, m, c, r0, n;
	printf("Enter the value of a:n");
	scanf("%d",&a);
	printf("Enter the value of m:n");
	scanf("%d",&m);
	printf("Enter the value of c:n");
	scanf("%d",&c);
	printf("Enter the value of r0(initial):n");
	scanf("%d",&r0);
	printf("Enter the no. of coin tosses you require:n");
	scanf("%d",&n);
	int tossResults[n];
	coinToss(r0, a, m, c, n, tossResults);
	int i;
	for(i=0;i<n;i++){
		printf("%d n",tossResults[i]);
	}
}

OUPUT:

Sample Run

You can even check to see if the coin is biased or not by finding out the number of Heads and Tails, after n tosses. Ideally they should be equal.

Code for that is given below.

CODE:

/********************************************
*********RANDOM NUMBER GENERATOR6*************
****************COIN TOSS*********************
********************************************/
#include<stdio.h>
#include<math.h>
/**Function that generates a random number.
Parameters: 
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
**/
int rand(int r0, int a, int m, int c){
	int r1=(a*r0+c)%m;
	return r1;
}
/**Function that generates random numbers given a seed, and stores them in an array that is passed as an argument.
Parameters:
r0: initial (first) seed
a: scale factor , so that a*r0 give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
n: no. of random numbers to be generated
x[n]: array that will store the random numbers
**/
void randomNos(int r0, int a, int m, int c, int n, int x[n]){
	int r1=rand(r0,a,m,c);;
	int i;
	for(i=0;i<n;i++){
		x[i]=r1;
		r1=rand(r1,a,m,c);
	}
}
/**Function that results the result of a coin toss:
Parameters:
r: a random number between 0 and 1
Returns 1 for Heads and 0 for tails
**/
int coinTossSingle(double r){
	if(r>0.5){
		return 1;
	} else if(r<0.5){
		return 0;	
	}
}
/**Function that generates n coin tosses results, given a seed and other starting conditions, and stores them in an array that is passed as an argument.
Parameters:
r0: initial (first) seed
a: scale factor , so that a*r0+c give the first random number
m: gives the max. value of random numbers that can be generated (m-1)
c: additional displacement factor
n: no. of coin tosses to be generated
x[n]: array that will store the random numbers
**/
void coinToss(int r0, int a, int m, int c, int n, int results[n]){
	int randNos[n];
	randomNos(r0, a, m, c, n, randNos);
	//Renormalize the random Nos. to [0 to 1]
	int i;
	double randNosNew[n];
	for(i=0;i<n;i++){
		randNosNew[i]=(double)randNos[i]/(m-1);
	}
	for(i=0;i<n;i++){
		results[i]=coinTossSingle(randNosNew[i]);	
	}
	
}
main(){
	int a, m, c, r0, n;
	printf("Enter the value of a:\n");
	scanf("%d",&a);
	printf("Enter the value of m:\n");
	scanf("%d",&m);
	printf("Enter the value of c:\n");
	scanf("%d",&c);
	printf("Enter the value of r0(initial):\n");
	scanf("%d",&r0);
	printf("Enter the no. of coin tosses you require:\n");
	scanf("%d",&n);
	int tossResults[n];
	coinToss(r0, a, m, c, n, tossResults);
	//Get the frequency distribution of coin tosses
	int i;
	int count[2];
	count[0]=0;
	count[1]=0;
	for(i=0;i<n;i++){
		if (tossResults[i]==1){
			count[0]++;
		} else{
			count[1]++;
		}
	}
	printf("\nThe number of Heads is: %d\nThe number of Tails is: %d\n ",count[0],count[1]);
}

OUPUT:

Sample Run

References and Resources:

https://cdsmith.wordpress.com/2011/10/10/build-your-own-simple-random-numbers/
https://en.wikipedia.org/wiki/Random_number_generation

https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator
Numerical Recipes in C

I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



Leave a Reply

Your email address will not be published. Required fields are marked *