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");
{

 

Link to comment
Share on other sites

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

int seqLen = 30;
int subSeqLen = 2;
int offset = 0;
i[]@seq;

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;
i[]@seq;

//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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...