Jump to content

Random value hold 1 or -1

Recommended Posts

Trying to find a little trick to return random selection of two specific values (1 or -1) and hold it for a number of frames or i in a loop

so it would be 1,1,1,-1,-1,-1,1,1,1,-1,-1,-1

or even better random ranges 1,1,1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,-1,-1

something like this but with better control of the distribution

for(int i=0; i<10; i++)
    float test = fit(floor(fit01(rand(i),-1,1)),-1,0,-1,1);
    int np = addpoint(0, pos);
    setpointattrib(0, "test", np, test, "set");


Share this post

Link to post
Share on other sites
Posted (edited)

For the case with same sub-sequence run length (run in Detail wrangle):

int seqLen = 30;
int subSeqLen = 2;
int offset = 0;

for (int i = 0; i < seqLen; i++){
 //this value is constant for the length of the sub sequence
 int seed = floor(i / subSeqLen);
 //rand returns 0-1 float
 //rint returns 0 or 1
 //math turns 0/1 to -1/1
 int value = (rint(rand(seed + offset)) * 2) - 1;
 push(@seq, value);

For the case with random run length:

int seqLen = 30;
int minRunLen = 2;
int maxRunLen = 10;
int runLenArr[];
int valueArr[];
int seed1 = 123;
int seed2 = 456;

//generate a smaller array to hold run lengths
//maximum length of this array is seqLen / minRunLen
for (int i = 0; i < seqLen / minRunLen; i++){
  int value = floor(fit(rand(i + seed1), 0, 1, minRunLen, maxRunLen));
  push(runLenArr, value);

//for each value in the run length array
for (int j = 0; j < len(runLenArr); j++){
  //generate that number of random numbers and add these values to valueArr
  for (int k = 0; k < runLenArr[j]; k++){
    int value = (rint(rand(runLenArr[j] + seed2)) * 2) - 1;
    push(valueArr, value);

//finally, copy the desired values into the target array
for (int m = 0; m < seqLen; m++){
  push(@seq, valueArr[m]);

There may be a better way to do this, but this is the thought process I used.

Edited by Fenolis

Share this post

Link to post
Share on other sites

thank you very much for the detailed response! will study these asap

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now