<Array>.random() returns [undefined]

Hello everybody!

Twine 2.3.15
Sugarcube 2.34.1
Firefox 91.11.0esr (64 bits) and Edge 102.0.1245.30 (64 bits)

I’m trying to use arrays to store a lot of first names in order to generate first names randomly.

Example array (my problem is with all arrays).

StoryInit

<<set $NomF_al to ["Anett","Angela","Anna","Antonia","Astrid","Barbara","Beate","Birgit","Claudia","Charlotte","Christina","Denise","Elisabeth","Erika","Eva","Franziska","Frieda","Gabriele","Gisela","Heike","Helene","Helga","Ingrid","Jana","Jutta","Karin","Katarina","Katja","Kornelia","Kristin","Laura","Lena","Magdalena","Margitta","Maria","Marion","Marita","Nadine","Petra","Silke","Stefanie","Theresia","Ulrike","Ursula","Ute","Utta","Vanessa"]>>

At some point in the game I’m asking the reader what’s the culture of the tribe. The list is work in progress, I plan to add more cultures than already present.

Passage 1

Vous utilisez des noms <<listbox "$Peuple_culture">><<option "allemands" "allemande">><<option "français" "française" selected>><<option "grecs" "grecque">><<option "romains" "romaine">><<option "russes" "russe">><</listbox>>

Later I create two new arrays, the idea is to have a shortcut to the cultural names.
Passage 2

<<switch $Peuple_culture>><<case "allemande">><<set $NomF to $NomF_al>><<set $NomM to $NomM_al>><<case "française">><<set $NomF to $NomF_fr>><<set $NomM to $NomM_fr>><<case "grecque">><<set $NomF to $NomF_he>><<set $NomM to $NomM_he>><<case "romaine">><<set $NomF to $NomF_la>><<set $NomM to $NomM_la>><<case "russe">><<set $NomF to $NomF_ru>><<set $NomM to $NomM_ru>><</switch>>

Later I generate names with a widget.

Here’s the widget (I’m aware from 2.36 it should be _args, but I’m still using 2.34)
Formules

<<widget "Notable">><<nobr>><<switch $Peuple_place_sexes>><<case "un matriarcat" "un matriarcat dur">><<set _sexe to 2>><<case "un patriarcat" "un patriarcat dur">><<set _sexe to 1>><<case "favorable aux hommes">><<set _sexe to either(1,1,1,2)>><<case "favorable aux femmes">><<set _sexe to either(1,2,2,2)>><<default>><<set _sexe to either(1,2)>><</switch>>
<<if _sexe == 1>><<set _nom to $NomM.random()>><<else>><<set _nom to $NomF.random()>><</if>>
<<set _age to random($args[1],$args[2]) + random($args[1],$args[2]) + random($args[1],$args[2])>>
_nom est <<switch _sexe>><<case 1>>un homme âgé<<default>>une femme âgée<</switch>> de _age hivers. $args[0]
<<set $Peuple_notables.push([_nom,_sexe,_age,$args[0]])>><</nobr>><</widget>>

My problem is that, sometimes, the .random() returns [undefined]. It’s a trouble I’ve experimented with each array, so it’s not a trouble from one specific array. It means it’s probably not a comma or quote problem. Example:

Is there a commonly known problem I’m not aware of about <Array>.random(), or maybe have I done something wrong?

I don’t see anything wrong with the code you’ve shown (of course, I might have missed something). I have created a project using that code, except that I only kept German names (and created a $NomM_al array with only three of them). I then put the widget about 40 times in one passage (using just the values <<Notable "1" "2" "3">>) and ran the game several times. I never got a name printed as _nom instead.

I’d also say it’s unlikely there is an issue with the formatting of your name arrays, as Twine displays error messages if there is a comma or quotation mark missing from an array.

Are you always initialising all of your name arrays? The only thing that comes to my mind looking at the screenshot is that it uses a case wit mixed gender (possibly the one with <<set _sexe to either(1,2,2,2)>>, but the array with male names was left empty, and the lone guy on the list generated as _nom (in my test, if I initialise the $NomM_al as an empty array, then run the widget – with the default settings, so a 50/50 split between male and female names – all of the men are “named” _nom. Because of how your screenshot is cropped, I can’t see if this is actually the case).

First, Agnieszka, I thank you for your effort!

I haven’t tested for choices leading to random sexe so far, only for either all female (matriarchy) or all male (patriarchy) characters yet.
What I showed is in fact the whole range of generated characters. From one test to another I’ve sometimes had no “_nom”, sometimes one and once or twice two. I suspect TME might have an explanation, or maybe GreyElf.

If they can’t offer one, it’s no big deal, I already have ideas how to proceed differently.

Yes, I’m also baffled by this one :wink: I hope someone more capable will have the answer - but it’s good to know you have a plan B just in case.

Out of curiosity, I tested around 30 times with female German names only and got no _nom. One difference is I’m using Sugarcube 2.36.1 - I wonder if this could affect things?

I will try to upgrade.

So, I’m now Twine 2.3.16 and Sugarcube 2.36.1. I’ve put a list of

<<print $NomM.random()>>

on a page, and from time to time I still get an [undefined] answer.

For some reason $NomM.length return 77 while there’s only 76 members. It might be the matter.

After a lengthy and thorough review of every instance I’ve typed the $NomM and/or the $NomF array I’ve spotted some not updated code from a previous way I did things: at some point in the past I created not two but three lists of first names, one for male, one for female and one of a few first names in French that can be given to both.
At some point I did include the third array in the male first names array and also in the female first names array. However, as I expanded my lists of first names to Greek language, it appeared there was no such thung as a name for both men or women. Each time the French transcription might have looked the same, the Greek names were in fact different. For instance, Antigone is in fact either Aντίγονος (male) or Aντιγόνη (female).

So all of this was an error of mine (which was one of the things I suspected). Agnieszka, I apologize for the time you wasted over my sheer incompetence!

No worries, glad you found out what was causing the issue!