Please help with Math.random use

Math.random use on startX/Y, rad, angle, etc...

Page 1 of 1

3 Replies - 938 Views - Last Post: 28 October 2009 - 05:59 PM Rate Topic: -----

#1 leosch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 09

Please help with Math.random use

Post icon  Posted 28 October 2009 - 05:33 PM

I am not sure when or where to use the Math.random. PLease help... thank you

/** Method to create a starburst
	 * 
	 */
public void star( int startX, int startY, int rad, double angle, Color newColor)
{
	Graphics g = this.getGraphics();
	double n= 360/angle;
	angle = Math.toRadians(angle);
	g.setColor(newColor);
	
		for (double i = 0; i < n; i++)
		{
			double x2 = startX+rad*Math.cos(angle*i);
			double y2 = startY+rad*Math.sin(angle*i);
			g.drawLine (startX,startY,(int)(x2), (int)(y2));
			
			
		}
		
}


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Please help with Math.random use

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Please help with Math.random use

Posted 28 October 2009 - 05:44 PM

when or where to use it, depends on what you want to do, and why do you need this random number.
so its hard to say, unless you add those details.
Was This Post Helpful? 0
  • +
  • -

#3 leosch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 09

Re: Please help with Math.random use

Posted 28 October 2009 - 05:51 PM

View Postjapanir, on 28 Oct, 2009 - 04:44 PM, said:

when or where to use it, depends on what you want to do, and why do you need this random number.
so its hard to say, unless you add those details.


Basically, I need my method params to be "random"... I am trying to figure it out.
Should I put the Math.random in the method (), or declare it in the code?

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Please help with Math.random use

Posted 28 October 2009 - 05:59 PM

again, it depends on how you design your program.
however both ways are ok.
in your case, your method gets the parameters, so you should use the random outside this method, the call it passing the values.
like:
int startX = //get random number
int startY = //get random number
//the rest of the parameters
//then call star
star(startX, startY/*etc.*/);



the other way is
public void star(){
//generating random numbers into startX, startY etc
//and then
g.drawLine (startX,startY,(int)(x2), (int)(y2));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1