Skip to content

Instantly share code, notes, and snippets.

@mattebb
Last active July 18, 2016 13:29
Show Gist options
  • Save mattebb/2c11f7e4f15ea20d5483 to your computer and use it in GitHub Desktop.
Save mattebb/2c11f7e4f15ea20d5483 to your computer and use it in GitHub Desktop.
Weighted random VEX
int i, steps = 80;
float sumhist[], sum=0, slice = 1.0/float(steps);
seed += 123456789;
for (i=0;i<steps;i++) {
float pos = (i*slice);
float val = chramp("__rndweight", pos);
sum += val;
push(sumhist, sum);
}
float rnd = random(seed) * sum;
// linear search
for(i=0;i<steps-1;i++) {
if (rnd > sumhist[i] && rnd <= sumhist[i+1])
break;
}
weight_random = float(i)/float(steps) + random(seed+123)*slice;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment