Counting occurrences of random selection

I have a button that randomly selects one of 4 things. As one repeatedly clicks, I’d like to keep track of how many times each of those 4 things came up, then display each frequency in a table. My problem is how to keep track of each count. In other coding I’m used to using a condition like, “when , a = a+1”. I’ve tried a similar approach but have not had success yet.

You can use a capture (whatever variable you’re using, I assume 1-4). Then, push the history to a list in a graph, and manipulate that list.
In button CL:
capture("things"): thingNumberVariable
GraphCL:
numberList(`T`): buttonName.history("things")
In graph:

T=[ ]
thing1=total(T[T=1])
thing2=total(T[T=2])
thing3=total(T[T=3])
thing4=total(T[T=4])
1 Like

Thank you, Daniel. I had to divide each total by the number being counted to get the frequency of each, but that was an easy fix. My problem now is that the tally is one-occurrence late with what I’m graphically showing. So, the button gets pressed and the graph component generates a random number and a corresponding animation occurs. But the button component doesn’t capture that new value until the button is pressed again, which triggers the next graph component selection and animation. I think I know why this is happening, but I haven’t figured out a solution yet. Will continue to try.

You can just display the last recorded value?

T[length(T)]

Yes, I was just about to explore that same idea, and it makes sense that it should work. It’ll be kind of like a boomerang connection between the graph and button components. I’ll reply if it works. I see there is a lastValue source, and that may accomplish the same as T[length(T)].

And speaking of length, that also simplified my frequency counts, using ‘length’ instead of ‘total’ in your initial example.

Thanks again, Daniel!

1 Like

It works! I think ricochet or ping-pong is a more appropriate metaphor than boomerang. Button triggers graph triggers button triggers graph, all with one click!

Oh right. I wasn’t really thinking when I used total. Good catch.

Is there a way to reset T back to T=[ ] with a separate reset button?

You can capture the button history from the repeated clicks, then capture the last one when you reset. This will give an offset from which to start the new count. There’s an example of this offset here:

This is a workaround to get a partial list.

Thank you, Susan. That description was helpful. I don’t know if I implemented in the same way you did, but I got it to work!

1 Like

Bonus points scored for getting it to work! I love that moment when it all comes together.

1 Like