# How to put this algorithm in my app?

**URL:** https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714
**Category:** Questions about Thunkable X
**Created:** [July 4, 2019, 2:05pm UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714 "2019-07-04T14:05:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yukold](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/yukold/32/35122_2.png) [@Yukold](https://community.thunkable.com/u/Yukold)
#### Post date: [July 4, 2019, 2:05pm UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714/1 "2019-07-04T14:05:15Z")

</div>

Hello again…

I think this is the most difficult question i have, because i don´t know how to do this…  
But i have a lot of information and maybe anyone can help me… alone will be impossible to me =(

I tryng to create a poker app… and in this game you can make deals, we usually do when there are 9 players left

The algorithm here:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/b/b/bbcc881c24992898b004c4bfc21cc8442960c0e1.png)  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/a/2a308ce23b00246b2f144b588d1d61c0308dda27.png)

Link from this images: [https://www.holdemresources.net/misc/pdf/evaluation\_of\_equity\_models.pdf](https://www.holdemresources.net/misc/pdf/evaluation_of_equity_models.pdf)  
Session 2.1

This is possible too:

Here is the algorithm:

Given n players  
Given stack sizes **S** = S1, S2, … Sn  
Given prizes **Z** = Z1, Z2, … Zn  
Get normalized stack sizes **Q** = Q1, Q2, …Qn by dividing **S** by average stack

Simulate 1 tournament:  
For each player i, generate a “place” for each player (Pi) by generating a random number between 0 and 1 and raising it to the power of 1/Qi. Pi = rand ^ (1.0 / Qi)  
Sort players by their place **P** – higher is better  
Award prizes **Z** according to **P**

This last information many people create this algorithm in 2 different language… maybe help…

Java:  
import java.util.Arrays;  
import java.util.Comparator;  
import java.util.Random;

public class SimulatedIcm {

```
private Random rnd = new Random();

public double[] getEquities(double[] payouts, double[] stacks,
		int maxIterations) {
	if (payouts.length > stacks.length)
		payouts = Arrays.copyOf(payouts, stacks.length);

	double[] equities = new double[stacks.length];
	double[] exp = getExponents(stacks);
	final double[] r = new double[stacks.length];

	Integer[] ids = new Integer[stacks.length];
	for (int i = 0; i < ids.length; i++)
		ids[i] = i;

	Comparator<Integer> comp = new Comparator<Integer>() {
		@Override
		public int compare(Integer i1, Integer i2) {
			if (r[i1] < r[i2])
				return 1;
			if (r[i1] > r[i2])
				return -1;
			return 0;
		}
	};

	for (int iteration = 0; iteration < maxIterations; iteration++) {
		for (int i = 0; i < r.length; i++)
			r[i] = Math.pow(rnd.nextDouble(), exp[i]);
		Arrays.sort(ids, comp);
		for (int i = 0; i < payouts.length; i++)
			equities[ids[i]] += payouts[i];
	}

	for (int i = 0; i < equities.length; i++)
		equities[i] /= (double) maxIterations;

	return equities;
}

private static double[] getExponents(double[] stacks) {
	double t = 0;
	for (double s : stacks)
		t += s;
	t /= (double) stacks.length;
	double[] exp = new double[stacks.length];
	for (int i = 0; i < stacks.length; i++)
		exp[i] = t / stacks[i];
	return exp;
}

public static void main(String[] args) {
	SimulatedIcm sicm = new SimulatedIcm();

	double[] payouts = new double[] { 50, 30, 20 };
	double[] stacks = new double[] { 1000, 2000, 3000, 4000, 5000, 6000,
			7000, 8000, 9000 };
	double[] eqs = sicm.getEquities(payouts, stacks, 1000000);
	System.out.println(Arrays.toString(eqs));
}

```

}

Python:  
from random import random

def trial(weights, payouts):  
scores = sorted((random() \*\* weight, i) for i, weight in enumerate(weights))  
results = [0] \* len(payouts)  
for payout, score in zip(payouts, scores): results[score[1]] = payout  
return results

def sicm(stacks, payouts, trials):  
avg = sum(stacks) / float(len(stacks))  
payouts += [0] \* (len(stacks) - len(payouts))  
payouts.sort()  
weights = [avg / s for s in stacks]  
return [sum(player) / float(trials) for player in zip(\*(  
trial(weights, payouts) for i in xrange(trials)  
))]

# I guess I should add an equivalent test to be fair to the Java implementation…

if **name** == ‘ **main** ’:  
print sicm([1000 \* n for n in range(1, 10)], [50, 30, 20], 1000000)

Link about this scripts: [https://forumserver.twoplustwo.com/15/poker-theory/new-algorithm-calculate-icm-large-tournaments-1098489/](https://forumserver.twoplustwo.com/15/poker-theory/new-algorithm-calculate-icm-large-tournaments-1098489/)

My app is here:  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/c/2c1994c4c3d746154012403ab0058eb1d8c6b05c.jpeg)

So the calculation will shows under ICM (Column 4)  
Anyone know how to do this?

You can take real proof of the calculation here:

> **[Poker ICM Calculator](https://www.icmpoker.com/icmcalculator/)**
>
> Easy-to-use online ICM calculator. Calculate ICM equity of player stacks for any given poker tournament payout structure.

or  
[https://www.holdemresources.net/icmcalculator](https://www.holdemresources.net/icmcalculator)

I just need finish him… =/

If i do anything wrong, sorry… i just dont know i can’t do this  
And so much thanks for anyone help me =) i have no ideia how do this.

---

<div class="post-metadata">

### Author: ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)
#### Post date: [July 5, 2019, 10:52am UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714/2 "2019-07-05T10:52:17Z")

</div>

Hey @Yukold!

> [@Yukold](#):
>
> I think this is the most difficult question i have,

I think I’d agree!! 😂

A couple of quick questions:

1. Do you have much/any experience with conditional probability or linear algebra?
2. Is there a particular reason you need to build your own chip model simulator? Perhaps there is an API that can do the heavy lifting for you?
3. Looking that the python script your have attached is _should_ be possible to do this by yourself, but I’d break it down into smaller steps or functions to get started.

---

<div class="post-metadata">

### Author: ![Yukold](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/yukold/32/35122_2.png) [@Yukold](https://community.thunkable.com/u/Yukold)
#### Post date: [July 5, 2019, 4:37pm UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714/3 "2019-07-05T16:37:01Z")

</div>

Hi Domhnall!!

Haha so much hard for for someone without so much knowledge  
So, answering your questions

I have a little experiencie with algebra in my school… a long time ago  
No problem if have anything else can do it for me… i just need something working well =)  
I put this python code for anyone understand and maybe can help me see this code, but i dont have any experience with this language or other, i never studied script/code or something else about it unfurtunately

---

<div class="post-metadata">

### Author: ![Yukold](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/yukold/32/35122_2.png) [@Yukold](https://community.thunkable.com/u/Yukold)
#### Post date: [July 9, 2019, 9:23am UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714/4 "2019-07-09T09:23:56Z")

</div>

Hey Domhnall, its possible do this algorithm in thunkable x? i tried many times but i maybe are thinking are impossible to do this…

---

<div class="post-metadata">

### Author: ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)
#### Post date: [November 8, 2024, 12:12pm UTC](https://community.thunkable.com/t/how-to-put-this-algorithm-in-my-app/131714/5 "2024-11-08T12:12:19Z")

</div>


