0% found this document useful (0 votes)
34 views11 pages

Nagu Rohit Chandra Maths Project Assign-1

The document contains two programming assignments: one for creating a random number generator in C and another for simulating the Monty Hall problem. The random number generator uses bitwise manipulation to produce unique numbers, while the Monty Hall simulation demonstrates the probability of winning by switching doors versus staying with the initial choice. Both assignments include detailed explanations of the code and the underlying concepts.

Uploaded by

rohit123ronal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

Nagu Rohit Chandra Maths Project Assign-1

The document contains two programming assignments: one for creating a random number generator in C and another for simulating the Monty Hall problem. The random number generator uses bitwise manipulation to produce unique numbers, while the Monty Hall simulation demonstrates the probability of winning by switching doors versus staying with the initial choice. Both assignments include detailed explanations of the code and the underlying concepts.

Uploaded by

rohit123ronal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NAGU ROHIT CHANDRA

2310110189
ASSIGNMENT 1:
1. Write your own code (any programming language) for
random number generator. Verify your output of the code if
the sequence of numbers generated is random or not.
CODE EXECUTION:
Manual Code in C:
#include <stdio.h>
#include <unistd.h> // For sleep function
#include <stdbool.h> // For boolean type
#include <stdlib.h> // For malloc and free
// Simple Random Number Generator using bitwise manipulation without seed
unsigned int simple_random_generator() {
static unsigned int state = 123456789; // Starting state (constant initial value)
// Evolve the state using basic arithmetic and bitwise operations
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;

return state;
}
bool is_unique(unsigned int number, unsigned int* generated_numbers, int count) {
for (int i = 0; i < count; i++) {
if (generated_numbers[i] == number) {
return false; // Number is a duplicate
}
}
return true; // Number is unique
}
int main() {
int n;
unsigned int random_num;
// Ask user for the number of random numbers to generate
printf("Enter the number of random numbers to generate (max 1000): ");
scanf("%d", &n);
// Check if n exceeds the range of unique numbers (0-999)
if (n > 1000) {
printf("Error: Cannot generate more than 1000 unique numbers in the range
0-999.\n");
return 1; // Exit the program with error code
}
unsigned int* generated_numbers = (unsigned int*)malloc(n * sizeof(unsigned
int)); // Dynamic array to store unique numbers
if (generated_numbers == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit the program with error code
}
int count = 0; // Count of unique numbers generated
printf("Generated Random Numbers (in range 0 to 999):\n");
// Generate 'n' random numbers using the custom random generator
while (count < n) {
random_num = simple_random_generator() % 1000; // Modulo 1000 to keep
numbers in range [0, 999]
if (is_unique(random_num, generated_numbers, count)) {
generated_numbers[count] = random_num; // Store the unique
number
printf("%u\n", random_num);
count++;
sleep(1); // Delay of 1 second
}
}
free(generated_numbers); // Free allocated memory
return 0;
}

OUTPUT:

EXPLANATION:
 <stdio.h>: This header file is included for performing standard input and output
operations.
 <unistd.h>: Provides access to the sleep function, allowing for a time delay between
actions.
 <stdbool.h>: This header is used to work with Boolean data types (true and false).
 <stdlib.h>: Used for memory management functions like malloc and free.

Random Number Generator Function:

simple_random_generator(): This function uses bitwise operations on a static variable,


state, to produce pseudo-random numbers. It modifies the state with every call, and does not
require a seed for randomization.

Uniqueness Checking Function:

is_unique(unsigned int number, unsigned int generated_numbers, int count):* This function
verifies whether the newly generated number already exists in an array of previously
generated numbers. It returns true if the number is unique, and false if it is a duplicate.

Main Function:

 Prompts the user to specify the number of random numbers they want to generate,
ensuring the input does not exceed 1000.
 Allocates memory dynamically to store these unique random numbers.
 Runs a loop to generate the random numbers, checking each one to ensure it is unique
before adding it to the array.
 Displays the unique numbers, pausing for 1 second between each print.
 After completing, the program frees the allocated memory and then exits.
[Link] Monty Hall problem and validate your
result with theoretical result. (Any
programming language)
CODE EXECUTION:
Manual Code in C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to simulate the Monty Hall problem


void monty_hall_simulation(int num_trials, int *stay_wins, int *switch_wins) {
int doors[3]; // Represents doors [0, 1, 2]
int car_door, contestant_choice, monty_opens, switch_choice;

*stay_wins = 0;
*switch_wins = 0;

for (int i = 0; i < num_trials; i++) {


// Randomly place the car behind one of the doors
car_door = rand() % 3;

// Contestant makes a random choice


contestant_choice = rand() % 3;

// Monty opens a door that has a goat and is not the contestant's choice
do {
monty_opens = rand() % 3;
} while (monty_opens == contestant_choice || monty_opens == car_door);

// Contestant switches to the other unopened door


for (int j = 0; j < 3; j++) {
if (j != contestant_choice && j != monty_opens) {
switch_choice = j;
break;
}
}

// If the contestant stays


if (contestant_choice == car_door) {
(*stay_wins)++;
}

// If the contestant switches


if (switch_choice == car_door) {
(*switch_wins)++;
}
}
}

int main() {
int num_trials;
int stay_wins, switch_wins;

// Take input from the user


printf("Enter the number of trials: ");
scanf("%d", &num_trials);

srand(time(0)); // Initialize random seed

monty_hall_simulation(num_trials, &stay_wins, &switch_wins);

// Calculate probabilities
double stay_win_prob_simulated = (double) stay_wins / num_trials;
double switch_win_prob_simulated = (double) switch_wins / num_trials;
double stay_win_prob_theoretical = 1.0 /3;
double switch_win_prob_theoretical = 2.0 /3;

// Print results
printf("Simulated probability of winning by staying: %lf\n",
stay_win_prob_simulated);
printf("Simulated probability of winning by switching: %lf\n",
switch_win_prob_simulated);
printf("Theoretical probability of winning by staying: %lf\n",
stay_win_prob_theoretical);
printf("Theoretical probability of winning by switching: %lf\n",
switch_win_prob_theoretical);

return 0;
}

OUTPUT:

EXPLANATION:
The Monty Hall problem is a well-known probability puzzle inspired by a game show.
Here's how it works:

The Setup:

 There are three doors, with one hiding a car (the prize) and the other two hiding goats
(non-prizes).
 The contestant selects one door, aiming to win the car.
 After the contestant makes their choice, the host (Monty) opens one of the remaining
two doors, revealing a goat.
 The contestant is then given the option to either stick with their initial choice or
switch to the other unopened door.

The Question:

What should the contestant do to improve their chances of winning the car—stick with their
original choice or switch?

Step-by-Step Analysis:

Initial Probabilities:

 The chance that the car is behind the door the contestant first picked is 1/3.
 The chance that the car is behind one of the two other doors is 2/3.

Monty's Role:

 Monty, knowing where the car is, always opens a door with a goat behind it (not the
one the contestant selected).
 This step adds information to the game, as Monty's choice to reveal a goat changes
the dynamics of the situation.

If the Contestant Stays:

 If the contestant keeps their original choice, the probability that they win the car
remains 1/3, since that was the chance when they first selected.

If the Contestant Switches:

 If they switch to the other door, their probability of winning increases to 2/3. This is
because Monty’s action (revealing a goat) shifts the 2/3 chance to the remaining
unopened door.

Why Switching is Better:

 Initially, there's a 1/3 chance that the contestant chose the car and a 2/3 chance they
picked a goat.
 Monty's reveal of a goat doesn’t change the original probabilities. Instead, it removes
one wrong option, leaving the unopened door with the higher probability.
 By switching, the contestant essentially moves to the door that now holds the 2/3
probability of hiding the car.

Probability Breakdown:

 Staying with the original choice means the probability of winning stays at 1/3.
 Switching to the other door increases the probability of winning to 2/3, as it
leverages the higher chance that the car was behind one of the unselected doors
initially.

Conclusion:

Switching doors gives the contestant a better chance of winning (2/3 probability) compared to
sticking with their initial choice (1/3 probability). This surprising result is why the Monty
Hall problem is so famous in probability theory!

You might also like