Skip to content

Commit 27d0a51

Browse files
committed
replace random() with choice(), add removeChoice(), remove PApplet param
1 parent a86f9ef commit 27d0a51

File tree

6 files changed

+93
-37
lines changed

6 files changed

+93
-37
lines changed

core/src/processing/data/DoubleList.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,25 @@ public double random() {
688688
}
689689

690690

691-
/**
692-
* Return a random value from the list, using the
693-
* randomSeed() from the specified sketch object.
694-
*/
695-
public double random(PApplet sketch) {
691+
// see notes in StringList
692+
// /**
693+
// * Return a random value from the list, using the
694+
// * randomSeed() from the specified sketch object.
695+
// */
696+
// public double random(PApplet sketch) {
697+
// if (count == 0) {
698+
// throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList");
699+
// }
700+
// return data[(int) sketch.random(count)];
701+
// }
702+
703+
704+
public double removeChoice() {
696705
if (count == 0) {
697706
throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList");
698707
}
699-
return data[(int) sketch.random(count)];
708+
int index = (int) (Math.random() * count);
709+
return remove(index);
700710
}
701711

702712

core/src/processing/data/FloatList.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,15 +710,24 @@ public float random() {
710710
}
711711

712712

713-
/**
714-
* Return a random value from the list, using the
715-
* randomSeed() from the specified sketch object.
716-
*/
717-
public float random(PApplet sketch) {
713+
// /**
714+
// * Return a random value from the list, using the
715+
// * randomSeed() from the specified sketch object.
716+
// */
717+
// public float random(PApplet sketch) {
718+
// if (count == 0) {
719+
// throw new ArrayIndexOutOfBoundsException("No entries in this FloatList");
720+
// }
721+
// return data[(int) sketch.random(count)];
722+
// }
723+
724+
725+
public float removeChoice() {
718726
if (count == 0) {
719-
throw new ArrayIndexOutOfBoundsException("No entries in this FloatList");
727+
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
720728
}
721-
return data[(int) sketch.random(count)];
729+
int index = (int) (Math.random() * count);
730+
return remove(index);
722731
}
723732

724733

core/src/processing/data/IntList.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -677,23 +677,33 @@ public void shuffle(PApplet sketch) {
677677
/**
678678
* Return a random value from the list.
679679
*/
680-
public int random() {
680+
public int choice() {
681681
if (count == 0) {
682682
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
683683
}
684684
return data[(int) (Math.random() * count)];
685685
}
686686

687687

688-
/**
689-
* Return a random value from the list, using the
690-
* randomSeed() from the specified sketch object.
691-
*/
692-
public int random(PApplet sketch) {
688+
// see notes in StringList
689+
// /**
690+
// * Return a random value from the list, using the
691+
// * randomSeed() from the specified sketch object.
692+
// */
693+
// public int choice(PApplet sketch) {
694+
// if (count == 0) {
695+
// throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
696+
// }
697+
// return data[(int) sketch.random(count)];
698+
// }
699+
700+
701+
public int removeChoice() {
693702
if (count == 0) {
694703
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
695704
}
696-
return data[(int) sketch.random(count)];
705+
int index = (int) (Math.random() * count);
706+
return remove(index);
697707
}
698708

699709

core/src/processing/data/LongList.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,27 @@ public void shuffle(PApplet sketch) {
676676
/**
677677
* Return a random value from the list.
678678
*/
679-
public long random() {
679+
public long choice() {
680680
return data[(int) (Math.random() * count)];
681681
}
682682

683683

684-
/**
685-
* Return a random value from the list, using the
686-
* randomSeed() from the specified sketch object.
687-
*/
688-
public long random(PApplet sketch) {
689-
return data[(int) sketch.random(count)];
684+
// see notes in StringList
685+
// /**
686+
// * Return a random value from the list, using the
687+
// * randomSeed() from the specified sketch object.
688+
// */
689+
// public long choice(PApplet sketch) {
690+
// return data[(int) sketch.random(count)];
691+
// }
692+
693+
694+
public long removeChoice() {
695+
if (count == 0) {
696+
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
697+
}
698+
int index = (int) (Math.random() * count);
699+
return remove(index);
690700
}
691701

692702

core/src/processing/data/StringList.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,25 +578,38 @@ public void shuffle(PApplet sketch) {
578578

579579

580580
/**
581-
* Return a random value from the list.
581+
* Return a random value from the list. Throws an exception if there are no
582+
* entries available. (Can't just return null because IntList and FloatList
583+
* can't do that, and would be inconsistent.)
582584
*/
583-
public String random() {
585+
public String choice() {
584586
if (count == 0) {
585587
throw new ArrayIndexOutOfBoundsException("No entries in this StringList");
586588
}
587589
return data[(int) (Math.random() * count)];
588590
}
589591

590592

591-
/**
592-
* Return a random value from the list, using the
593-
* randomSeed() from the specified sketch object.
594-
*/
595-
public String random(PApplet sketch) {
593+
// removing in 4.0.2, seems like overkill... if something this specific is
594+
// needed, then better to use a more elaborate/pedantic setup anyway.
595+
// /**
596+
// * Return a random value from the list, using the
597+
// * randomSeed() from the specified sketch object.
598+
// */
599+
// public String choice(PApplet sketch) {
600+
// if (count == 0) {
601+
// throw new ArrayIndexOutOfBoundsException("No entries in this StringList");
602+
// }
603+
// return data[(int) sketch.random(count)];
604+
// }
605+
606+
607+
public String removeChoice() {
596608
if (count == 0) {
597609
throw new ArrayIndexOutOfBoundsException("No entries in this StringList");
598610
}
599-
return data[(int) sketch.random(count)];
611+
int index = (int) (Math.random() * count);
612+
return remove(index);
600613
}
601614

602615

core/todo.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ contribs
1212
X P3D & P2D window not showing on MacOS Ventura (thx @jaegonlee)
1313
X https://github.com/processing/processing4/issues/544
1414

15+
data
16+
X use choice() instead of random() for list classes
17+
_ add choice() as a PApplet method for int values of random()?
18+
19+
contrib
1520
_ Loading SVG file gives Illegal base64 character 20 encoding error (worked in v3.5.4)
1621
_ https://github.com/processing/processing4/issues/592
1722
_ https://github.com/processing/processing4/issues/592
1823

24+
1925
_ freeze on resize with Windows (even the default renderer)
2026
_ https://github.com/processing/processing4/issues/507
2127

2228
_ need option for alphabetical ordering for json serialization
23-
_ use choice() instead of random() for list classes
24-
_ add choice() as a PApplet method for int values of random()?
2529

2630
_ concurrent StringDict et al
2731
_ why no concurrent TreemMap? https://stackoverflow.com/a/17656453

0 commit comments

Comments
 (0)