I need to run a coin toss simulation where I toss a coin 100 times, then run this experiment 100 times while plotting my number of Heads and Tails for each time the experiment is ran. Is there anyone that can help me design a code to conduct this experiment and present a graph of the results?

# Tag: simulation

## c++ – Simulation of a falling ball

I am new to C++ programming. Made a simple simulation of a falling ball. Is it possible to somehow improve or optimize this code? What tips can you give for development?

```
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int timeFallingBall() {
static int time(0);
return ++time;
}
void fallingBall() {
cout << "From what height do we drop the ball? (In meters): ";
double h;
cin >> h;
double v = 0, high = 0, s = 0, maxv = 0, g = 9.8;
int t = 0;
double hi = h;
while (h) {
t = timeFallingBall();
v = static_cast<double>(t) * g;
high = (v * static_cast<double>(t)) / 2;
h = hi - high;
if (h <= 0) {
switch (t) {
case 1:
cout << "After " << t << " second the ball has reached the ground! Maximum speed: " << maxv << " m/s";
break;
default:
cout << "After " << t << " seconds the ball has reached the ground! Maximum speed: " << maxv << " m/s";
}
break;
}
switch (t) {
case 1:
cout << "After " << t << " second, the ball is at a distance of " << h << " m from the ground at a speed: " << v << " m/s" << endl;
break;
default:
cout << "After " << t << " seconds, the ball is at a distance of " << h << " m from the ground at a speed: " << v << " m/s" << endl;
break;
}
if (v > maxv) {
maxv = v;
}
}
}
int main() {
system("chcp 1251>nul");
fallingBall();
return 0;
}
```
```

## c++ – Software Design – Hardware Abstraction Layer and Simulation

I want to design a Hardware Abstraction Layer (library) for two purposes

- Ease of use (e.g. messaging between different boards, etc.)
- Reuse of HAL interfaces in simulation (with different implementations)

The target language for hardware is C++11, while I want to be able to use C++17 features when using simulation specific interfaces and implementations.

What would be the best way to layout such a system. I’m thinking about design choices like

- two libraries vs. one (due to simulation specific code)
- minimal code duplication
- dynamic software configuration (i.e. strategy design pattern)
- minimal use of preprocessor switches
- clear directory structure for hardware and simulation specific code
- etc.

Memory is not a hardware bottleneck.

## simulation – How to find estimates and bias from simulated data using method of least square estimation

```
f((Alpha)_, (Lambda)_) :=
ProbabilityDistribution((
4*(Lambda)*(Alpha)*
x^((Alpha) - 1)*(1 - x)*(2 - x)^((Alpha) -
1)*(1 - x^(Alpha)*(2 - x)^(Alpha))^((Lambda) -
1))/(1 + (1 - x^(Alpha)*(2 - x)^(Alpha))^(Lambda))^2, {x, 0,
1}, Assumptions -> (Alpha) > 0 && (Lambda) > 0)
n = 10
(Kappa) = 1000
x = Table(RandomVariate(f(2, 0.5), n), {i, (Kappa)})
logl = Table(Sum(((
1 - (1 - x((i))^(Alpha)*(2 - x((i)))^(Alpha))^(Lambda))/(
1 + (1 - x((i))^(Alpha)*(2 - x((i)))^(Alpha))^(Lambda)) - i/(
n + 1))^2, {i, n}),{i, (Kappa)})
FindMinimum({logl, (Alpha) > 0, (Lambda) > 0}, {{(Alpha), 2}, {(Lambda), 0.5}})
```

I tried the above code to find the estimates and bias after simulating the data. For that ,I generated the sample from pdf. Then specified the function which is to be minimised, but i am unable to find the estimates. Could anyone please help me to correct the code to find the bias and estimates. As the sample size increases, bias to be decreased.

## Robot movement simulation [How to convert Java code to Python]

I am trying to solve this leetcode problem. Even though I solved it, I would like to see how I could translate my Java code to Python, since I am learning the language and its OOP components. How would the translated code look like?

This is the solution:

```
class Solution {
public boolean judgeCircle(String moves) {
Robot robot = new Robot(0, 0);
for (int i = 0; i < moves.length(); i++) {
char direction = moves.charAt(i);
robot.move(direction);
}
return robot.isAtOrigin();
}
}
class Robot {
public static final char UP = 'U';
public static final char DOWN = 'D';
public static final char LEFT = 'L';
public static final char RIGHT = 'R';
private final Point ORIGIN = new Point(0, 0);
private Point position;
public Robot(int x, int y) {
this.position = new Point(x, y);
}
public void move(char direction) {
switch(direction) {
case RIGHT:
position.x++;
break;
case LEFT:
position.x--;
break;
case UP:
position.y++;
break;
case DOWN:
position.y--;
break;
default:
}
}
public boolean isAtOrigin() {
return position.equals(ORIGIN);
}
}
class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (!(o instanceof Point)) {
return false;
}
Point other = (Point) o;
return (this.x == other.x) && (this.y == other.y);
}
}
```

## unity – Photon delay simulation make(game button lag animation)

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

## N body simulation packages for mathematica

Are there n body simulation packages available for Mathematica like Python, Fortran or other languages?

I want a package that will take the interparticle force and initial position, momentum as input, and give the final position, momentum of particles as output.

## simulation – How to solve this question on Fault collapsing?

Thanks for contributing an answer to Computer Science Stack Exchange!

- Please be sure to
*answer the question*. Provide details and share your research!

But *avoid* …

- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

## simulation – Building up functions from data sheet

I’m trying to use a list of data points from a csv file to perform multiple calculations and plot the end result. Basically I have 3 columns of data in my csv, I need the second and third to be pulled in as N1 and N2, and then the first column (wavelength) needs to be the list of x values I am plotting against. I’m completely lost with how to make this loop work.

```
lines = 100; (Theta)t = Table(i, {i, lines}); wavelength =
Table(i, {i, lines});
N1 = Table(i, {i, lines}); N2 = Table(i, {i, lines}); k1 =
Table(i, {i, lines}); k2 = Table(i, {i, lines});
rs = Table(i, {i, lines}); rp = Table(i, {i, lines}); Rs =
Table(i, {i, lines}); Rp = Table(i, {i, lines}); n1 =
Table(i, {i, lines}); n2 = Table(i, {i, lines});
```

```
degrees = 45;
(Theta)i = degrees*Pi/180;
Ravg0 = Table(i, {{i, lines} {j, 2}});
z = 1;
While(z < (lines),
wavelength((z)) = s((z, 1));
N2((z)) = s((z, 2));
k2((z)) = s((z, 3));
n1((z)) = 1 + k1((z))*I;
n2((z)) = N2((z)) + k2((z))*I;
(Theta)t((z)) = ArcSin((Sin((Theta)i)*(n1((z))/n2((z)))));
rs((z)) = -Sin((Theta)i - (Theta)t((z)))/
Sin((Theta)i + (Theta)t((z)));
rp((z)) =
Tan((Theta)i - (Theta)t((z))/Tan((Theta)i + (Theta)t((z)));
Rs((z)) = rs((z))*SuperStar(rs((z)));
Rp((z)) = rp((z))*SuperStar(rp((z)));
Ravg0((z, 2)) = (Rs((z)) + Rp((z)))/2;
Ravg0((z, 1)) = wavelength((z));
z++));
ListPlot(Ravg0, PlotRange -> All, Joined -> True)
```

Some of the errors I am recieving:

```
Set::partd: Part specification Ravg0((z,2)) is longer than depth of object.
Set::partd: Part specification Ravg0((z,1)) is longer than depth of object.
Set::partd: Part specification Ravg0((z,2)) is longer than depth of object.
General::stop: Further output of Set::partd will be suppressed during this calculation.
Set::partw: Part 3 of {i,i} does not exist.
Set::partw: Part 3 of {i,i} does not exist.
Set::partw: Part 4 of {i,i} does not exist.
```

Again, I’m completely lost as to what any of this means, and I know very little about mathematica, so any help would be appreciated.

## Elevator Simulation in C++ – Code Review Stack Exchange

So I wrote this elevator program in c++ and it is functioning quite well. However, it will always prioritize the lowest floor regardless of proximity. I am not sure how to optimized it so that it will prioritize the direction of the closer end. By end, I mean the first and last floor on the sorted list. Here is what I mean:

Currently, the programs output is this:

Elevator is currently at floor 10.

Passenger inputs are floors 1, 2, 3, and 11.

Elevator goes down, drops passenger at 1, then 2, then 3, then 11.

Ideally, since floor 11 is closer to 10 than floor 1, I would like it to go like this:

Elevator is currently at floor 10.

Passenger inputs are floors 1, 2, 3, and 11.

Elevator should go up, drop the passenger at 11, then go down, drop the passenger at 3, then 2, then 1.

Here is the code:

```
#include <iostream>
// including windows.h/unistd to use Sleep()/sleep() that will simulate a delay
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
// using a Doubly Linked List
struct node {
int data;
node *next;
node *prev;
};
node *head = NULL;
node *last = NULL;
node *current = head;
// function to display all constructed totalFloors
void displayFloors() {
node *temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// function to make the actual totalFloors
void constructFloors(int totalNodes) {
for (int i = 1; i <= totalNodes; i++) {
node *newNode = new node;
last = head;
newNode->data = i;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
current = head;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}
}
}
// function to move the elevator
void moveElevator(int destination) {
if (current->data == destination) {
cout << "You are already at floor " << destination << endl;
} else if (current->data <= destination) {
cout << "The elevator is going up....." << endl;
Sleep(1500);
while (current->data < destination) {
if (current->next)
current = current->next;
Sleep(1500);
cout << "Elevator is now at Floor....." << current->data << endl;
}
cout << "You have arrived at your destination." << endl;
} else if (current->data >= destination && destination >= head->data) {
cout << "The elevator is going down....." << endl;
Sleep(1500);
while (current->data > destination) {
current = current->prev;
Sleep(1500);
cout << "Elevator is now at Floor....." << current->data << endl;
}
if (current->prev)
current = current->prev;
cout << "You have arrived at your destination." << endl;
}
}
int main() {
int passenger(4), totalFloors, floor, continueFlag = 1;
cout << "This is an Elevator Simulator." << endl
<< "Due to the pandemic, the elevator will also" << endl
<< "simulate a maximum four-person capacity." << endl
<< "Input a number of floors to construct for the elevator " << endl
<< "or input a character to end simulation: ";
cin >> totalFloors;
if (!totalFloors) {
cout << "Simulation ended. Have a great day!" << endl;
return 0;
}
constructFloors(totalFloors);
cout << "The following floors have been created: ";
displayFloors();
cout << "The elevator is currently at Floor " << current->data << endl;
cout << "Please enter your destinations respectively "
<< "or enter a character to end simulation." << endl;
while (continueFlag == 1) {
int i, j, x;
for (i = 0; i < 4; i++) {
cout << "Passenger " << i + 1
<< ", please enter your destination: ";
cin >> passenger(i);
if (!passenger(i)) {
cout << "Simulation ended. Have a great day!" << endl;
return 0;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i - 1; j++) {
if (passenger(j) > passenger(j + 1)) {
int temp = passenger(j);
passenger(j) = passenger(j + 1);
passenger(j + 1) = temp;
}
}
}
for (i = 0; i < 4; i++) {
cout << passenger(i) << " ";
}
cout << endl;
for (x = 0; x < 4; x++) {
floor = passenger(x);
moveElevator(floor);
}
cout << "The elevator is now taking in new passengers." << endl;
}
cout << "Simulation ended. Have a great day!" << endl;
return 0;
}
```

Also, feel free to improvise any of my existing codes if possible.