Problems with the random() function

I’m using the random() function to simulate the roll of two dice. A button invokes two independent function calls for dice 1 and dice 2 and tracks combined results of the two die and also the number of doubles rolled.

The number of doubles rolled is 33%, TWICE the expected 16.67%. This skews the number of times I get a combined result of 2 or 12 in particular. I’m wondering if I’m implementing this wrong? Or is there some problem with calling random() in quick succession? Please see the shared doc: https://coda.io/@alanlarsen/random-test

Thanks for any helps or ideas!

1 Like

Please share the the whole random() formulas .

it can’t show formulas on the shared doc.

Thanks.

Hi @Alan_Larsen1,
welcome to Coda community! :handshake:

I agree with you this is not the expected behaviour.
As @Steve_Yang was suggesting, having your Random() formula implementation (and its statistics) would be easier (either by posting it directly or giving sharing the document in edit mode, not the published link).

I see from the Random Reference that updateContinuously is default set to true; I guess (but it’s not fully clear) that this is changing at every document load the seed: have you already tried to set it to false?

1 Like

I think the issue is the share link rights. @Federico_Stefanato

it can’t edit column width(sometimes the text is not wrap, can’t see the whole column name ), can’t see formula , and can’t comment by select text and table) .

Thanks all for your quick comments on this and sorry about the link! I’ve added the code at the bottom of the page and a link to the doc with edit rights:

Thanks @Federico_Stefanato. I have tried both true and false on the random function. If false, the values only change with the first button press.

Hmmm…
I think I found the issue…
@Alan_Larsen1, I added a section and it should now work.

Problem is that Random() is always the same in the same “context”.
In the same row, Random() will always return the same value in all the columns. It also happens in the formula.
This is why I did the multiple rows Random trick.
@Codans: is this what is intended?

Further, I added some intermediate results to better debug.
Now statistics seem to converge to their expected values.

Let me know if it helps.
Cheers!

Great catch and work-around @Federico_Stefanato! I knew something was going on, but couldn’t put my finger on it. I hope @Codans will fix this so calls to random() are always independent, but this works great for now - the actuals converge to the expected percentages as expected. Thank you!!

1 Like

Hi @Alan_Larsen1,

Welcome! And that’s an interesting find!
I’ve added this to our internal tracker so we can look into it more.

And thank you @Federico_Stefanato for tracking down where this was happening.

2 Likes

What is the “multiple rows random trick” and how do I get some of that??

You are totally right, but I don’t have the faintest idea :grimacing:
My memory wipes out after every 11 seconds, this was 5 months ago…
Let me figure out what I did

1 Like

I wish I could, but the doc is published and not allowed for editing…

@Alan_Larsen1, would you be so kind to let us see the underlying implementation?

Ah! Now I get it!
In the second page (https://coda.io/@alanlarsen/random-test/random-2-2), I created a table (Randoms) with two rows looked-up by the Test 2 table in two different columns (Random 1 and Random 2).
This allows to have two different random numbers because they belong to different rows and therefore applying each one per each dice.

I don’t know if this is relevant to your use case, but I suspect that the “Random() context” of the automation triggers the same random number every time.
While, pushing manually the button creates a new “Random() context” at each iteration.

This has been fixed, so new docs should have random numbers generated. Most older docs have been upgraded as well.

1 Like

Ok, so this takes out this potential explanation…

Hi, I’m still having this problem. When using the ‘values for new row function’ instead of a column function.

Hi @Diacom_Hoofddorp
and welcome to Coda Community! :handshake:
AFAIK, the problem should be sorted out, now.
If you put Random() as default value for a column, a different number should be picked (I just tested it).

Is it possible to share your doc or a sample of it?

Thank you.

It’s still a problem for columns using a values for new row function.

Hi @Diacom_Hoofddorp
I understand what you say, now: in the same row, you have the same Random() call.

Doing some tests I discovered quite a weird thing…
If you put Random(false) (with literal) in one column and Random(False()) (with function) in the other, you do have two different seeds.
So: if you have two columns, the problem is sorted out.

However, you can’t have other variations (such as Random(True().Not()) is behaving like Random(False()).

@Codans, what about adding a standard Random(seed:long) function to overcome with this?

1 Like