<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #random - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23random</link>
      <pubDate>Sun, 08 Aug 2021 20:34:21 +0000</pubDate>
         <description>Tagged with #random - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23random/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Create a box randomly flying away, then randomly flying back to the oringinal position.</title>
      <link>https://forum.processing.org/two/discussion/26417/create-a-box-randomly-flying-away-then-randomly-flying-back-to-the-oringinal-position</link>
      <pubDate>Sun, 18 Feb 2018 04:59:28 +0000</pubDate>
      <dc:creator>libandp</dc:creator>
      <guid isPermaLink="false">26417@/two/discussions</guid>
      <description><![CDATA[<p>Hi Guys~!</p>

<p>I've created a box can randomly flying away, but I want it randomly flying back when it outside the screen. Could you help me~?</p>

<pre lang="javascript">

float x= 50;
float y= 50;
float m1;
float m2;


float xspeed =1;
float yspeed =1;

void setup() {
  size(700, 700);
 
  frameRate(60);
}

void draw() {
   m1 =  random(3);
   m2 =  random(3);
  background(255);

  //1st rect
  x = x+xspeed;
  y = y+yspeed;


  fill(0);
    if (mousePressed &amp;&amp; (mouseButton == LEFT)) { //make random movement
      xspeed = xspeed + m1*random(-1, 1);
      yspeed = yspeed + m2*random(-1, 1);
    }
  
    if (mousePressed &amp;&amp; (mouseButton == RIGHT)) { // stop movement
      xspeed = 0;
      yspeed = 0;

    }


  strokeWeight(0.2);
  stroke(100);
  rect(x, y, 10, 10);
  println(x);
}
</pre>

<p>Thank you very much!!</p>
]]></description>
   </item>
   <item>
      <title>Fast sin() , random() and hsb() unit tests</title>
      <link>https://forum.processing.org/two/discussion/24214/fast-sin-random-and-hsb-unit-tests</link>
      <pubDate>Fri, 22 Sep 2017 07:27:32 +0000</pubDate>
      <dc:creator>prince_polka</dc:creator>
      <guid isPermaLink="false">24214@/two/discussions</guid>
      <description><![CDATA[<p>EDIT:</p>

<p>If someone could just run the sin() test and post their results than would be very interesting. 
This is my result</p>

<pre><code>Speed test starting!
Just the loop itself took ... 14 milliseconds!
fastsin() took ... 80 milliseconds!
regularmodulo took ... 148 milliseconds!
Math.sin() took ... 208 milliseconds!

Accuracy test starting ...
2000000 tests was performed
Average deviation was 1.5023357863701483E-16
The maximum deviation was 1.1934897514720433E-15
</code></pre>

<p>I'm very confused why using %  for modulo is so much slower than the fastmod function.</p>

<p>I wrote a fast sin() approximation and a test for it and wonder if it's just faster on my computer?</p>

<p>Also tested export application and write to file and the results are the same.</p>

<p>I read about intrinsic functions that may or may not be used.</p>

<p>Also I have an idea for a fast random() function if that would be useful?</p>

<p>Or some other heavily used function that could be faster.</p>

<p>Anyways here is the sketch.</p>

<p>Also tested export application and write to file and the results are the same.</p>

<pre><code>final static double baseline(double x) { return x; }
final static double fastmod(double a, double b){return a - b*(long)(a/b);}
private final static double 
 invfact3 = - 1 / 6D, 
 invfact5 =   1 / 120D,
 invfact7 = - 1 / 5040D,
 invfact9 =   1 / 362880D,
invfact11 = - 1 / 39916800D,
invfact13 =   1 / 6227020800D,
invfact15 = - 1 / 1307674368000D,
invfact17 =   1 / 355687428096000D,
invfact19 = - 1 / 121645100408832000D,
invfact21 =   1 / 51090942171709440000D,
invfact23 = - 1 / 25852016738884976640000D,
invfact25 =   1 / 15511210043330985984000000D,
invfact27 = - 1 / 10888869450418352160768000000D,
invfact29 =   1 / 8841761993739701954543616000000D,

PI = Math.PI,
TWO_PI = Math.PI*2,

increment = 0.00001
;
final static double fastsin(double x) {
  final double x0 = fastmod(x,TWO_PI); 
                x = fastmod(x,PI); 
  final double x2=x*x;
    x = x 
    + (x*=x2) * invfact3
    + (x*=x2) * invfact5
    + (x*=x2) * invfact7
    + (x*=x2) * invfact9
    + (x*=x2) * invfact11
    + (x*=x2) * invfact13
    + (x*=x2) * invfact15
    + (x*=x2) * invfact17
    + (x*=x2) * invfact19
    + (x*=x2) * invfact21
    + (x*=x2) * invfact23
    + (x*=x2) * invfact25
    + (x*=x2) * invfact27
    + (x*=x2) * invfact29
    ;
    if(Math.abs(x0)&gt;PI){ x=-x; }
    return x;
}
final static double regularmodulo(double x) {
  double x0 = x%TWO_PI; 
              x %= Math.PI; 
  final double x2=x*x;
    x = x 
    + (x*=x2) * invfact3
    + (x*=x2) * invfact5
    + (x*=x2) * invfact7
    + (x*=x2) * invfact9
    + (x*=x2) * invfact11
    + (x*=x2) * invfact13
    + (x*=x2) * invfact15
    + (x*=x2) * invfact17
    + (x*=x2) * invfact19
    + (x*=x2) * invfact21
    + (x*=x2) * invfact23
    + (x*=x2) * invfact25
    + (x*=x2) * invfact27
    + (x*=x2) * invfact29
    ;
    if(Math.abs(x0)&gt;PI){ x=-x; }
    return x;
}
void setup(){
  noLoop();
}
void draw(){
  long start,stop;
  double foo=0,bar=0;
  println("Speed test starting!");
  print("Just the loop itself took ... ");
  start = System.nanoTime();
  for(int i=-1000000;i&lt;1000000;i++){
  foo+=baseline(i*increment);
  }
  stop = System.nanoTime();
  println((stop-start)/1000000 + " milliseconds!");
  bar+=foo;
  foo=0;
  print("fastsin() took ... ");
  start = System.nanoTime();
  for(int i=-1000000;i&lt;1000000;i++){
  foo+=fastsin(i*increment);
  }
  stop = System.nanoTime();
  println((stop-start)/1000000 + " milliseconds!");
  bar+=foo;
  foo=0;
  print("regularmodulo took ... ");
  start = System.nanoTime();
  for(int i=-1000000;i&lt;1000000;i++){
  foo+=regularmodulo(i*increment);
  }
  stop = System.nanoTime();
  println((stop-start)/1000000 + " milliseconds!");
  bar+=foo;
  foo=0;
  print("Math.sin() took ... ");
  start = System.nanoTime();
  for(int i=-1000000;i&lt;1000000;i++){
  foo+=Math.sin(i);
  }
  stop = System.nanoTime();
  println( (stop-start)/1000000 + " milliseconds!");
  bar+=foo;
  println(bar==1?".":"");
  println("Accuracy test starting ...");
  long tests = 0;
  double maxdeviation = 0;
  double totaldeviation = 0;
  double dev = 0;
  for(int i=-1000000;i&lt;1000000;i++, tests++){
  dev = Math.abs(Math.sin(i*increment)-fastsin(i*increment));
  if(dev&gt;maxdeviation)maxdeviation = dev;
  totaldeviation+=dev;
  }
  println( tests + " tests was performed"  );
  println("Average deviation was " + totaldeviation/tests );
  println("The maximum deviation was " + maxdeviation );
}
</code></pre>

<p>xorshitstar , RNG known for it's speed,  <a href="https://en.wikipedia.org/wiki/Xorshift" target="_blank" rel="nofollow">https://en.wikipedia.org/wiki/Xorshift</a><br />
It's supposed to use unsigned integer types but java only provides signed types.</p>

<p>Testing the quality of random numbers is tricky since there is different metrics.<br />
I tested "squared deviation from the mean" and a visual comparison against random() generating static by random colors seems to work fine.</p>

<p>It works just like random() with two arguments,  but returns double rather than a float.<br />
For example, if you want a random float between 0 - 10 you do <code>(float)xorshitstar(0,10)</code><br />
<code>(float)xorshitstar(-25.5,-17.7)</code>  etc ...</p>

<p>The X should be a global variable and initialized with a seed, for example<br />
or a constant if you want deterministic randomness.</p>

<pre><code>final static long STAR = 2685821657736338717L;
long X;

final double xorshiftstar(double start, double end){
  X ^= X &gt;&gt; 12;
  X ^= X &lt;&lt; 25;
  X ^= X &gt;&gt; 27;
  X *= STAR;
  return start + Math.abs( ( X* Math.abs(end-start) )/Long.MAX_VALUE  ) ;
}
final static int dumb(int x) { return x; }
int upperhalf,bottom;
void setup(){
 size(200,400);
 RANDOM_LONG = System.nanoTime(); 
 //int[] all = new int[71];
 long start=0,stop=0;
 println("Speed test starting!");

 print("Just the loop itself took ... ");
 start = System.nanoTime();
 for(int i=0;i&lt;100000000;i++){
   dumb(i);
 }
 stop = System.nanoTime();
 println((stop-start)/1000000 + " milliseconds!");

 print("xorshitstar took ... ");
 start = System.nanoTime();
 for(int i=0;i&lt;100000000;i++){
   xorshiftstar(-100000,100000);
 }
 stop = System.nanoTime();
 println((stop-start)/1000000 + " milliseconds!");

 print("random() took ... ");
 start = System.nanoTime();
 for(int i=0;i&lt;100000000;i++){
   random(-100000,100000);
 }
 stop = System.nanoTime();
 println((stop-start)/1000000 + " milliseconds!");

 print("Math.random() took ... ");
 start = System.nanoTime();
 for(int i=0;i&lt;100000000;i++){
   Math.random();
 }
 stop = System.nanoTime();
 println((stop-start)/1000000 + " milliseconds!");

 println("Quality test starting!");
 println("Beginning with static.");
 upperhalf = width*height/2;
 bottom = width*height;
 textAlign(CENTER);
 textSize(24);
 loadPixels();
 fill(0);
}
int[] qualityxor,qualityrand;
long scoreXor,scoreRand;
int winsXor=0,winsRand=0;
void draw(){
  if(frameCount&lt;600){
  for(int i=0;i&lt;upperhalf;i++){
    pixels[i]=(int)random(#000000,#ffffff);
  }
  for(int i=upperhalf;i&lt;bottom;i++){
    pixels[i]=(int)xorshiftstar(#000000,#ffffff);
  }
  updatePixels();
  text("random()",width*.5,height*.25);
  text("xorshiftstar()",width*.5,height*.75);
  }
  else{
    background(170);
    qualityxor = new int[6];
    qualityrand = new int[6];
    scoreXor = 0;
    scoreRand = 0;
    for(int i=0;i&lt;60000000;i++){
     qualityxor[(int)xorshiftstar(0,6)]++;
     qualityrand[(int)random(0,6)]++;
  }

  for(int i=0;i&lt;6;i++){
     int tx = Math.abs( qualityxor[i]-10000000);
     int tr = Math.abs(qualityrand[i]-10000000);
     scoreXor+=tx*tx;
     scoreRand+=tr*tr;
  }
  println("Comparing squared deviation from the mean of dice throws");
  if(scoreRand&lt;scoreXor)
  println("random() won, and has " + (++winsRand) + " wins total!");
  else if (scoreXor&lt;scoreRand)
  println("xorshiftstar() won, and has " + (++winsXor) + " wins total!");
  else println("OMG A TIE! Go buy a lottery ticket now!!! ");
  println("xorshiftstar() score : " +  scoreXor);
  println("      random() score : " +  scoreRand);
  println("Lower score is better!");
  println("Press the R-key for rematch");
  text("random : " + winsRand,width*.5,height*.25);
  text("xorshiftstar : " + winsXor,width*.5,height*.75);

  noLoop();

  }
}
void keyPressed(){if(keyCode=='r' || keyCode=='R')loop();}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Never mind, found the mistake. Thanks for the view.</title>
      <link>https://forum.processing.org/two/discussion/25092/never-mind-found-the-mistake-thanks-for-the-view</link>
      <pubDate>Sun, 19 Nov 2017 16:45:55 +0000</pubDate>
      <dc:creator>kathleenjudith</dc:creator>
      <guid isPermaLink="false">25092@/two/discussions</guid>
      <description><![CDATA[<p>Hi there :)
What I'm trying to do:
I have an array of PImages that are supposed to first disappear when being clicked and then the next PImage of that array is supposed to appear on a random position.
Also, I'm not using the actual cursor but an image of a cursor that moved in opposite directions. So, if the actual cursor is going left, the fake on is going right, and so on.</p>

<p>To start of I made this, which actually works. Here I'm just using a rectangle instead of the PImages.</p>

<pre><code>float posX = 0;
float posY = 0;
float rectW = 100;
float rectH = 100;

PImage cursor;

void setup(){
  size(800,600);
  noCursor();
  cursor = loadImage("cursor.png");
}

void draw(){
  background(255);
  cursor.resize(16,0);

  rect(posX, posY, rectW, rectH);
  image(cursor, width-mouseX, height-mouseY);
}

void mousePressed(){

  if(width-mouseX &gt; posX &amp;&amp; width-mouseX &lt; (posX + rectW) &amp;&amp; height-mouseY &gt; posY &amp;&amp; height-mouseY &lt; (posY + rectH)){

    posX = random(0, width-rectW);
    posY = random(0, height-rectH);
    }
}
</code></pre>

<p>But when replacing the rectangle with the array PImage, is doesn't work anymore and I can't seem to make it work, I've been trying for days.</p>

<pre><code>float posX = 0;
float posY = 0;
float insultW = 100;
float insultH = 100;

PImage cursor;
PImage[] insults;

int index = 0;

void setup(){
  size(800,600);
  noCursor();

  insults = new PImage[6];
  for(int i = 0; i &lt; insults.length; i++){
    insults[insults.length-1 -i] = loadImage("insult_"+i+".jpg");
  }

  cursor = loadImage("cursor.png");
}

void draw(){
  background(255);

  //rect(posX, posY, rectW, rectH);

  image(insults[index], posX, posY);

  image(cursor, width-mouseX, height-mouseY, insultW, insultH);
  cursor.resize(16,0);
}

void mouseClicked(){

  if(width-mouseX &gt; posX &amp;&amp; width-mouseX &lt; (posX + insultW) &amp;&amp; height-mouseY &gt; posY &amp;&amp; height-mouseY &lt; (posY + insultH)){

    posX = random(0, width-insultW);
    posY = random(0, height-insultH);

    index++;
    if(index&gt;=6){
    index = 0;
    }
}
}
</code></pre>

<p>Please help me.</p>
]]></description>
   </item>
   <item>
      <title>How to get rid of predictable random() results</title>
      <link>https://forum.processing.org/two/discussion/23807/how-to-get-rid-of-predictable-random-results</link>
      <pubDate>Mon, 14 Aug 2017 13:06:58 +0000</pubDate>
      <dc:creator>Rouzbelious</dc:creator>
      <guid isPermaLink="false">23807@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys,</p>

<p>I am trying to make a surface using ISurface command of igeo library and I used a 2d array of vector points to create the surface and I set a random value for z parameter of each point. but it appears random function is not that much random and it generates repetitive values. so anyone knows how to get really random values?</p>
]]></description>
   </item>
   <item>
      <title>Principle behind the working of random function</title>
      <link>https://forum.processing.org/two/discussion/23679/principle-behind-the-working-of-random-function</link>
      <pubDate>Wed, 02 Aug 2017 15:39:58 +0000</pubDate>
      <dc:creator>vinayvarma189</dc:creator>
      <guid isPermaLink="false">23679@/two/discussions</guid>
      <description><![CDATA[<p>Hey everyone,
This is my first question at this forum,so please excuse if this is in an informal way.
The thing I am wondering about is how does a random function work?.I know What a random function does,but how does it bring a random number, i mean how is the computer programmed to bring a random number? :-? . can anyone help.</p>
]]></description>
   </item>
   <item>
      <title>How to Link generated objects with a moving character?</title>
      <link>https://forum.processing.org/two/discussion/22847/how-to-link-generated-objects-with-a-moving-character</link>
      <pubDate>Wed, 31 May 2017 19:54:36 +0000</pubDate>
      <dc:creator>dcolwell18</dc:creator>
      <guid isPermaLink="false">22847@/two/discussions</guid>
      <description><![CDATA[<p>I am creating a code in which we are trying to link a character, in this case Mario, with a sequence of randomly generated blocks. We have created a system of generation for the blocks, but are having trouble having Mario interact with them and stop on the blocks. Any suggestions? Currently the blocks are generated on a series of timers, and his interactions with them are controlled by a series of if statements determining Mario's position. Any suggestions?</p>
]]></description>
   </item>
   </channel>
</rss>