# Math random non repeating numbers

Page 1 of 1

## 9 Replies - 8035 Views - Last Post: 18 January 2012 - 08:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=263430&amp;s=026c4d74301f0a501d5e806c72c0d9ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jamey-Wright

Reputation: 0
• Posts: 5
• Joined: 17-January 12

# Math random non repeating numbers

Posted 18 January 2012 - 07:35 PM

Hey guys! I need help, I'm creating a program (and no it's not homework before you ask ) and I have 6 players who type "roll" and I use the math random command to assign a random number between one and 6. How do I make it so that if a player already gets a number it's not repeated for the next player. I already tried
```if (number1 == number2)
```
and it didn't work.Plus that only helps if there are 2 players.

Is This A Good Question/Topic? 0

## Replies To: Math random non repeating numbers

### #2 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Math random non repeating numbers

Posted 18 January 2012 - 07:41 PM

Maybe you could use an array

Roll dice
Roll dice
If array contains result
roll again

OR

You create an array of {1,2,3,4,5,6} and remove the index value from the list when it is rolled.

eg.

Roll dice
remove the index of the result, example : 5, remove the fifth element, which is five, now roll another random number up to 5
then to 4
then to 3
so on...

Edit, you'll want a list, not an array since a list can add or remove elements, preventing the need to resize arrays.

This post has been edited by Mylo: 18 January 2012 - 07:42 PM

### #3 Jamey-Wright

Reputation: 0
• Posts: 5
• Joined: 17-January 12

## Re: Math random non repeating numbers

Posted 18 January 2012 - 07:43 PM

Mylo, on 18 January 2012 - 07:41 PM, said:

Maybe you could use an array

Roll dice
Roll dice
If array contains result
roll again

OR

You create an array of {1,2,3,4,5,6} and remove the index value from the list when it is rolled.

eg.

Roll dice
remove the index of the result, example : 5, remove the fifth element, which is five, now roll another random number up to 5
then to 4
then to 3
so on...

Edit, you'll want a list, not an array since a list can add or remove elements, preventing the need to resize arrays.

Could you possibly give me an example. I know it says not to give out code but believe me I've been trying.

### #4 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Math random non repeating numbers

Posted 18 January 2012 - 07:51 PM

Something like this:

```	public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<Integer>();
int[] playerscores = new int[6];

for (int i = 0; i < playerscores.length; ++i)
{
int rnd = (int) (Math.random() * numbers.size());
playerscores[i] = numbers.get(rnd);
numbers.remove(rnd);
}

for (int x : playerscores)
{
System.out.println(x);
}
}

```

Or even another way is to just shuffle the list of possible values, and assign them to the respective player.

This post has been edited by Mylo: 18 January 2012 - 07:52 PM

### #5 Jamey-Wright

Reputation: 0
• Posts: 5
• Joined: 17-January 12

## Re: Math random non repeating numbers

Posted 18 January 2012 - 07:58 PM

Mylo, on 18 January 2012 - 07:51 PM, said:

Something like this:

```	public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<Integer>();
int[] playerscores = new int[6];

for (int i = 0; i < playerscores.length; ++i)
{
int rnd = (int) (Math.random() * numbers.size());
playerscores[i] = numbers.get(rnd);
numbers.remove(rnd);
}

for (int x : playerscores)
{
System.out.println(x);
}
}

```

Or even another way is to just shuffle the list of possible values, and assign them to the respective player.

Thanks. I'll try this out.

### #6 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,541
• Joined: 05-April 11

## Re: Math random non repeating numbers

Posted 18 January 2012 - 08:03 PM

I am following the same idea as Mylo's, but have changed it to how I would have implemented it

```private ArrayList<Integer> numbers;
private Random rnd = new Random();

//Add the numbers 1-6 to the numbers arraylist
public void fillArrayList() {
numbers = new ArrayList<Integer>();
for (int i=1; i<=6; i++)
}

public int roll() {
if (numbers.size() > 0)
return numbers.remove(rnd.nextInt(numbers.size()));
return -1; //The array is empty!
}

```

This post has been edited by CasiOo: 18 January 2012 - 08:03 PM

### #7 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Math random non repeating numbers

Posted 18 January 2012 - 08:09 PM

That would be alot better ^ Didn't even know remove returned a value =)

### #8 Jamey-Wright

Reputation: 0
• Posts: 5
• Joined: 17-January 12

## Re: Math random non repeating numbers

Posted 18 January 2012 - 08:09 PM

CasiOo, on 18 January 2012 - 08:03 PM, said:

I am following the same idea as Mylo's, but have changed it to how I would have implemented it

```private ArrayList<Integer> numbers;
private Random rnd = new Random();

//Add the numbers 1-6 to the numbers arraylist
public void fillArrayList() {
numbers = new ArrayList<Integer>();
for (int i=1; i<=6; i++)
}

public int roll() {
if (numbers.size() > 0)
return numbers.remove(rnd.nextInt(numbers.size()));
return -1; //The array is empty!
}

```

Thanks. I'm not amazing with Java. What I'm trying to create is a Russian roulette program. I need it so 6 users enter their names, type 'roll' and then are assigned a number between one and six. Then the players pull the trigger in ascending order. I'm finding it very difficult to make, because I am so new. So far I only have the code where users enter their name and then roll the dice.

### #9 pbl

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

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

## Re: Math random non repeating numbers

Posted 18 January 2012 - 08:13 PM

It is there since almost 3 years and has been mentionned many times

http://www.dreaminco...snippet2992.htm

### #10 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,541
• Joined: 05-April 11

## Re: Math random non repeating numbers

Posted 18 January 2012 - 08:15 PM

Jamey-Wright, on 18 January 2012 - 08:09 PM, said:

CasiOo, on 18 January 2012 - 08:03 PM, said:

I am following the same idea as Mylo's, but have changed it to how I would have implemented it

```private ArrayList<Integer> numbers;
private Random rnd = new Random();

//Add the numbers 1-6 to the numbers arraylist
public void fillArrayList() {
numbers = new ArrayList<Integer>();
for (int i=1; i<=6; i++)
}

public int roll() {
if (numbers.size() > 0)
return numbers.remove(rnd.nextInt(numbers.size()));
return -1; //The array is empty!
}

```

Thanks. I'm not amazing with Java. What I'm trying to create is a Russian roulette program. I need it so 6 users enter their names, type 'roll' and then are assigned a number between one and six. Then the players pull the trigger in ascending order. I'm finding it very difficult to make, because I am so new. So far I only have the code where users enter their name and then roll the dice.

Just keep on trying and working hard!
Don't expect every project to be a success, I personally think that 90% of my projects are complete trash, but I learn something new in every project