# Math.random()

Page 1 of 1

## 8 Replies - 1169 Views - Last Post: 24 April 2011 - 05:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=229040&amp;s=c00c82c22999265c3b3ce20c23dddfc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 muhteremmuho

Reputation: -2
• 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

• D.I.C Lover

Reputation: 1576
• 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

### #3 muhteremmuho

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

## Re: Math.random()

Posted 24 April 2011 - 06:40 AM

CasiOo, 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

### #4 CasiOo

• D.I.C Lover

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

## Re: Math.random()

Posted 24 April 2011 - 07:07 AM

muhteremmuho, on 24 April 2011 - 06:40 AM, said:

CasiOo, 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 )

### #5 muhteremmuho

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

## Re: Math.random()

Posted 24 April 2011 - 07:19 AM

CasiOo, on 24 April 2011 - 07:07 AM, said:

muhteremmuho, on 24 April 2011 - 06:40 AM, said:

CasiOo, 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

### #6 Sinned

Reputation: 19
• 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)
```

### #7 muhteremmuho

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

## Re: Math.random()

Posted 24 April 2011 - 07:29 AM

Thank you .I succeed

### #8 g00se

• D.I.C Lover

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

## Re: Math.random()

Posted 24 April 2011 - 08:09 AM

### #9 pbl

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

Reputation: 8378
• 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)
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