We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › getting a better random number
Page Index Toggle Pages: 1
getting a better random number (Read 791 times)
getting a better random number
May 11th, 2009, 9:33am
 
Hey been using random() but its not very random at all and tends to stick to the top right of my sketch,

is there a better way to get a better random number?

code im using

 float posX = random(0,900);
 float posY =random(0,600);


shape(bot[magic],posX,posY);

Dave
Re: getting a better random number
Reply #1 - May 11th, 2009, 10:48am
 
random is random... can you paste some more of your code... maybe the problem is somewhere else
Re: getting a better random number
Reply #2 - May 11th, 2009, 10:50am
 

 String message;

 color col1, col2;
 float duration;
//Date now = new Date();
//long seed = now.getTime();
 
 float posX = random(0,900);
 float posY =random(0,600);
 float yy = random(0,8);
 float posZ = random(-10,100);
 
 float zoom = map(100, 0, width, 0.5, 1.5);
 float b = random(20,100);
 PShape[] bot =  new PShape[7];
 int magic = (int)random(0,7);
 ColorMove(String m, color c1, color c2, float d)
 {
 
   
   message = m;
   col1 = c1;
   col2 = c2;
   duration = d;
   bot[0] = loadShape("circles2.svg");
   bot[1] = loadShape("circles3.svg");
   bot[2] = loadShape("circles4.svg");
   bot[3] = loadShape("adict.svg");
   bot[4] = loadShape("authority.svg");
   bot[5] = loadShape("bush.svg");
   bot[6] = loadShape("circles.svg");
   
 }
 void Draw(int frame)
 {
   float a = frame / (GetDuration() * frameRate);
   pushStyle();
   fill(lerpColor(col1, col2, a));
  // float posX = lerp(width - textWidth(message), 0, a);
  // float posY = lerp(height - BASE_DISPLAY_FONT_SIZE, 0, a);
  PFont font = loadFont("Helvetica-Bold-48.vlw");
  textFont(font,24);
 
 
   text(message, posX, posY);
   
   scale(zoom);
   shape(bot[magic],posX+posZ,posY);
   popStyle();
 }
 float GetDuration()
 {
   return duration;
 }
}
Re: getting a better random number
Reply #3 - May 11th, 2009, 12:02pm
 
To bad i still can not test your code. but there are two things you should test.

First. Why do you add posZ to posX, that adds another 0-100 to the x coordinate and moves it to the right. The other problem could be your shape. try to change http://processing.org/reference/shapeMode_.html
shapeMode from CORNER to CENTER, at least be sure that your shape is exactly where you want it to be when you draw it not random, test it with, height/2,width/2 for example.  If its CORNER Mode, the left upper Corner is at the Center, not the Shape itself.
Probably its both that moves your shapes...

If it still doesnt work... tell me
Re: getting a better random number
Reply #4 - May 11th, 2009, 1:36pm
 
want it to be randomly placed on the screen, width/2 height/2 just keeps it in the middle.

i added the z to push it further to the right as it was hanging about on the left
Re: getting a better random number
Reply #5 - May 11th, 2009, 1:52pm
 
i know that it just place them in the middle... that was for bugfixing to see if your shapes really are placed in the middle... or if there is a offset... see. you had to correct it manually, but random doesnt make sense then. So does it work?
Re: getting a better random number
Reply #6 - May 11th, 2009, 2:30pm
 
tried both of those but the shapes keep to the left and occasiaonlly go to the far left.

So the random isnt working properly ?
Re: getting a better random number
Reply #7 - May 11th, 2009, 10:33pm
 
Remarks outside of the immediate problem:
I already suggested to move the loadShapes outside of these classes, to the main class' setup. Likewise, you should move the loadFont there, putting such instruction in the draw routine is a frame rate killer! Smiley

As for random, I am not sure, it depends on the size of the shapes. Maybe you should compensate the coordinates to take in account their scaled size.
BTW, the scale instruction can shift coordinates, too.
Re: getting a better random number
Reply #8 - May 11th, 2009, 11:33pm
 
tried putting it in the main class setup but got a bunch of erros when i tried to pass them to the class
Re: getting a better random number
Reply #9 - May 12th, 2009, 2:26am
 
Declare the variables above the setup().
Initialize them within.

Code:
PShape[] bot =  new PShape[7];
PFont oneFont;

void setup()
{
 // Usual stuff
 ;
 // Init
 bot[0] = loadShape("...");
bot[0].scale(wantedSize);
 // and so on
 oneFont = createFont("...");
}

Note I scale the shape just after load: if they keep the same size in the program, it should save some time.
Re: getting a better random number
Reply #10 - May 12th, 2009, 2:39am
 
Didnt you get my PM? I offered you to help you with your code.  if you dont wanna send me your whole code, maybe you can trim it but make it runable so i can check where the problem is.
Re: getting a better random number
Reply #11 - May 12th, 2009, 3:27am
 
Cedric, I think Dave is in a hurry so might have not noticed your kind offer.
For what it is worth, the latest version I sent to him is at http://www.autohotkey.net/~PhiLho/PhiLhoSoft/ShowDBMessages.zip
Or http://bazaar.launchpad.net/~philho/+junk/Processing/files/head:/ShowDBMessages/ for a cutting edge version! Wink (that's a Bazaar branch.)
You need a MySQL database to run it although it is possible to tweak it to bypass this need (getting messages from an array or a file for example).
Dave tries to plug his own drawing routines there.
Re: getting a better random number
Reply #12 - May 12th, 2009, 3:32am
 
Oh ok, i didnt realize it was that complex. Never mind.  he probably just didnt read my message. Did you solve the problem?
Re: getting a better random number
Reply #13 - May 12th, 2009, 8:18am
 
Where is your size(sizex, sizey) line?
How big is your output window?

Code:
float posX = random(0,900); // Maybe 900 is too big. Try "width"
float posY =random(0,600); // Maybe 600 is too small. Try "height"
float yy = random(0,8);
float posZ = random(-10,100); // Average: +45

// zoom depends on how big "width" is.
float zoom = map(100, 0, width, 0.5, 1.5);

/* ... */

scale(zoom);  // What value is zoom? I
shape(bot[magic],posX+posZ,posY);  // adding posZ moves to the right by posZ


I suggest you don't use a posZ, and am guessing you want to use "zoom" to adjust the size of your shape, in which case you should realise that shape() has 5 parameters, allowing you to specify the location as well as the size.

-spxl
Page Index Toggle Pages: 1