1

Does anybody know if there is any equivalent to

import random
random.choices(population, weitghts, k=1)

.. for Python Mode for Processing?.

I want to deploy an algorithm for this StackOverflow question, but can't find the desired function.

Cheche
  • 1,456
  • 10
  • 27
  • It doesn't look like this is really Python at all. [The reference on mentions a few trivial randomness related APIs, with minimal Python overlap](https://py.processing.org/reference/). – ShadowRanger Nov 09 '18 at 02:49

1 Answers1

1

Python mode for Processing is built on Jython, and Jython still hasn't crawled out of the Python 2 era (it only moved to Python 2.7 compatibility, from 2.5, in 2015, half a decade after 2.7 released).

random.choices was introduced in Python 3.6; even if Python mode for Processing offered full access to all Python features (and I have a hard time determining if it does), Jython's 2.7 compatibility means it has almost no features introduced after Python 3.1, so random.choices is not available without reimplementing it from scratch. Of course, the code for random.choices is open source and posted online, so you could always just backport it to Python 2 yourself (along with itertools.accumulate, which it relies on).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271