8 Replies - 1169 Views - Last Post: 24 April 2011 - 05:45 PM Rate Topic: -----

#1 muhteremmuho  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 165
  • Joined: 06-January 11

Math.random()

Posted 24 April 2011 - 06:24 AM

I want to create three values with random method.but these values must be different between themselves.how can I do this?
int x=(int)(Math.random()*5);
int y=(int)(Math.random()*5);
int z=(int)(Math.random()*5);
X,Y,Z must be different.thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Math.random()

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1576
  • View blog
  • Posts: 3,548
  • Joined: 05-April 11

Re: Math.random()

Posted 24 April 2011 - 06:30 AM

you must do a loop where you check if they equal each other, if they are then continue to do the loop

int x, y, z;

while (true) {
	x = (int)(Math.random()*5);
	
	if ( x != y && x != z )
		break;
}


This post has been edited by CasiOo: 24 April 2011 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 muhteremmuho  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 165
  • Joined: 06-January 11

Re: Math.random()

Posted 24 April 2011 - 06:40 AM

View PostCasiOo, on 24 April 2011 - 06:30 AM, said:

you must do a loop where you check if they equal each other, if they are then continue to do the loop

int x, y, z;

while (true) {
	x = (int)(Math.random()*5);
	
	if ( x != y && x != z )
		break;
}


an error.can u write x,y,z with random .illegal starting ex. there is
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1576
  • View blog
  • Posts: 3,548
  • Joined: 05-April 11

Re: Math.random()

Posted 24 April 2011 - 07:07 AM

View Postmuhteremmuho, on 24 April 2011 - 06:40 AM, said:

View PostCasiOo, on 24 April 2011 - 06:30 AM, said:

you must do a loop where you check if they equal each other, if they are then continue to do the loop

int x, y, z;

while (true) {
	x = (int)(Math.random()*5);
	
	if ( x != y && x != z )
		break;
}


an error.can u write x,y,z with random .illegal starting ex. there is


? Sorry I don't understand.

You could also do a loop like while ( x == y || x == z || y == z )
Was This Post Helpful? 1
  • +
  • -

#5 muhteremmuho  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 165
  • Joined: 06-January 11

Re: Math.random()

Posted 24 April 2011 - 07:19 AM

View PostCasiOo, on 24 April 2011 - 07:07 AM, said:

View Postmuhteremmuho, on 24 April 2011 - 06:40 AM, said:

View PostCasiOo, on 24 April 2011 - 06:30 AM, said:

you must do a loop where you check if they equal each other, if they are then continue to do the loop

int x, y, z;

while (true) {
	x = (int)(Math.random()*5);
	
	if ( x != y && x != z )
		break;
}


an error.can u write x,y,z with random .illegal starting ex. there is


? Sorry I don't understand.

You could also do a loop like while ( x == y || x == z || y == z )

ERROR:"illegal start of type" for "while(true){" row
Was This Post Helpful? 0
  • +
  • -

#6 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 208
  • Joined: 13-October 10

Re: Math.random()

Posted 24 April 2011 - 07:20 AM

This will work:
int x=(int)(Math.random()*5);
		int y=(int)(Math.random()*5); 
		int z=(int)(Math.random()*5);
		
		x=(int)(Math.random()*5); // you don't have to compare it with anything
		while(y==x) y=(int)(Math.random()*5); // have to compare with the previous set var (x)
		while(z==x||z==y) z=(int)(Math.random()*5); // have to compare with the previous set vars (x, y)

Was This Post Helpful? 0
  • +
  • -

#7 muhteremmuho  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 165
  • Joined: 06-January 11

Re: Math.random()

Posted 24 April 2011 - 07:29 AM

Thank you .I succeed
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,135
  • Joined: 20-September 08

Re: Math.random()

Posted 24 April 2011 - 08:09 AM

Try http://technojeeves....-random-numbers
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Math.random()

Posted 24 April 2011 - 05:45 PM

That is the way I will do it

ArrayList<Integer> al = new ArrayList<Integer>();
for(int i = 0; i < 5; ++i)
   al.add(i);
Random r = new Random();

int x = al.remove(r.nextInt(al.size()));
int y = al.remove(r.nextInt(al.size()));
int z = al.remove(r.nextInt(al.size()));


But my "simple" solution might be completly wrong in your case :)

This post has been edited by pbl: 24 April 2011 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1