Hi all,
I'm new in programing.I'm trying to assign random numbers which math.random will choose.Each time I run program Math.random must choose either 50,100 or 150
ie: 50*1 for 1, 50*2 for 2, 50*3 for 3. I hope my explanation makes sense.

Thank you!

int width;
	   
	    
	    
	    width = (int)(Math.random() *3+1);

What happens when you execute the code you show? do you get the desired values?

I'm trying to change width of triangles to produce tree.
Thank you

Triangle triangle1;
    	Triangle triangle2;
    	Triangle triangle3;
    	Square trunk1;
    	Square trunk2;
    	

		triangle1 = new Triangle();	
		triangle1.changeSize(50, width1);
		triangle1.moveHorizontal(25);
		triangle1.moveVertical(70);
		triangle1.makeVisible();

Do you have any questions or problems with this new section of code? Without the class definitions, no one will be able to help.

What happened to the random problem?

I' m trying to use math.random to resize width of triangles. Width must be either 50,100 or 150.
Thank you,

int width;   	    
       width = (int)(Math.random() *3+1);       
	    
	    
	Triangle triangle1;
    	Triangle triangle2;
    	Triangle triangle3;
    	Square trunk1;    	
         Square trunk2; 

         triangle1 = new Triangle();
         triangle1.changeSize(50, width);
         triangle1.moveHorizontal(25);
	triangle1.moveVertical(70);	
 	triangle1.makeVisible();
Member Avatar for coil

Once you assign a random value from 1 to 3 to width, just scale it by multiplying by 50.

In the future, if the values needed aren't consecutive and also don't differ by a constant value (unlike this case), you could use a switch-case to manually assign different values.

Pseudocode:

int rnd=Math.random()*5; //assuming you need 5 different numbers
int width;

switch(rnd) {
case 0: width=5; //or whatever value you want
case 1: width=8;
case 2: width=10;
...
}

Thank you Coil,
It is giving other size other than 50,100 and 150. Should i change something else?

int width;   	    
       width = (int)(Math.random() *3+1);       
	    
	    
	Triangle triangle1;
    	Triangle triangle2;
    	Triangle triangle3;
    	Square trunk1;    	
         Square trunk2; 

         triangle1 = new Triangle();
         triangle1.changeSize(50, width*50);
         triangle1.moveHorizontal(25);
	triangle1.moveVertical(70);	
 	triangle1.makeVisible(); int width;

nevermind, it is working now. thank you again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.