Skip to content

Commit f054b4c

Browse files
committed
avoid repetition in multirandom #196
1 parent 7b69ec0 commit f054b4c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

docs/Basic Usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Prompting is, primarily, just text input. However, there are some special option
8282
- An entry can contain the syntax of eg `1-5` to automatically select a number from 1 to 5. For example, `<random:1-3, blue>` will give back any of: `1`, `2`, `3`, or `blue`.
8383
- You can repeat random choices via `<random[1-3]:red, blue, purple>` which might return for example `red blue` or `red blue purple` or `blue`.
8484
- You can use a comma at the end like `random[1-3,]` to specify the output should have a comma eg `red, blue`.
85+
- This will avoid repetition, unless you have a large count than number of options.
8586
- You can use the syntax `<wildcard:my/wildcard/name>` to randomly select from a wildcard file, which is basically a pre-saved text file of random options, 1 per line.
8687
- Edit these in the UI at the bottom in the "Wildcards" tab.
8788
- You can also import wildcard files from other UIs (ie text file collections) by just adding them into `Data/Wildcards` folder.

src/Text2Image/T2IParamInput.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,31 @@ static T2IParamInput()
111111
return null;
112112
}
113113
string separator = data.Contains("||") ? "||" : (data.Contains('|') ? "|" : ",");
114-
string[] vals = data.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
115-
if (vals.Length == 0)
114+
string[] rawVals = data.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
115+
if (rawVals.Length == 0)
116116
{
117117
Logs.Warning($"Random input '{data}' is empty and will be ignored.");
118118
return null;
119119
}
120120
string result = "";
121+
List<string> vals = rawVals.ToList();
121122
for (int i = 0; i < count; i++)
122123
{
123-
string choice = vals[context.Random.Next(vals.Length)];
124+
int index = context.Random.Next(vals.Count);
125+
string choice = vals[index];
124126
if (TryInterpretNumberRange(choice, out string number))
125127
{
126128
return number;
127129
}
128130
result += context.Parse(choice).Trim() + partSeparator;
131+
if (vals.Count == 1)
132+
{
133+
vals = rawVals.ToList();
134+
}
135+
else
136+
{
137+
vals.RemoveAt(index);
138+
}
129139
}
130140
return result.Trim();
131141
};
@@ -142,13 +152,24 @@ static T2IParamInput()
142152
Logs.Warning($"Wildcard input '{data}' does not match any wildcard file and will be ignored.");
143153
return null;
144154
}
155+
WildcardsHelper.Wildcard wildcard = WildcardsHelper.GetWildcard(card);
145156
List<string> usedWildcards = context.Input.ExtraMeta.GetOrCreate("used_wildcards", () => new List<string>()) as List<string>;
146157
usedWildcards.Add(card);
147158
string result = "";
159+
List<string> vals = wildcard.Options.ToList();
148160
for (int i = 0; i < count; i++)
149161
{
150-
string choice = WildcardsHelper.PickRandom(card, context.Random);
162+
int index = context.Random.Next(vals.Count);
163+
string choice = vals[index];
151164
result += context.Parse(choice).Trim() + partSeparator;
165+
if (vals.Count == 1)
166+
{
167+
vals = wildcard.Options.ToList();
168+
}
169+
else
170+
{
171+
vals.RemoveAt(index);
172+
}
152173
}
153174
return result.Trim();
154175
};

0 commit comments

Comments
 (0)