9 Replies - 3622 Views - Last Post: 18 January 2012 - 08:15 PM Rate Topic: -----

#1 Jamey-Wright  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Please help! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Math random non repeating numbers

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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
Add result to array
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

Was This Post Helpful? 0
  • +
  • -

#3 Jamey-Wright  Icon User is offline

  • New D.I.C Head

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

Re: Math random non repeating numbers

Posted 18 January 2012 - 07:43 PM

View PostMylo, on 18 January 2012 - 07:41 PM, said:

Maybe you could use an array

Roll dice
Add result to array
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.
Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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>();
		numbers.add(1);
		numbers.add(2);
		numbers.add(3);
		numbers.add(4);
		numbers.add(5);
		numbers.add(6);
		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

Was This Post Helpful? 0
  • +
  • -

#5 Jamey-Wright  Icon User is offline

  • New D.I.C Head

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

Re: Math random non repeating numbers

Posted 18 January 2012 - 07:58 PM

View PostMylo, on 18 January 2012 - 07:51 PM, said:

Something like this:

	public static void main(String[] args)
	{
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		numbers.add(1);
		numbers.add(2);
		numbers.add(3);
		numbers.add(4);
		numbers.add(5);
		numbers.add(6);
		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.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • 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++)
		numbers.add(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

Was This Post Helpful? 1
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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 =)
Was This Post Helpful? 0
  • +
  • -

#8 Jamey-Wright  Icon User is offline

  • New D.I.C Head

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

Re: Math random non repeating numbers

Posted 18 January 2012 - 08:09 PM

View PostCasiOo, 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++)
		numbers.add(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.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Math random non repeating numbers

Posted 18 January 2012 - 08:15 PM

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

View PostCasiOo, 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++)
		numbers.add(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 ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1