5 Replies - 504 Views - Last Post: 30 April 2015 - 01:26 PM Rate Topic: -----

#1 selereth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-April 15

How to prevent randomizer from picking same index in an array twice

Posted 29 April 2015 - 12:19 PM

import java.util.*;

public class DungeonsAndDragonsRedux {

	public static void main(String[] args) {
		Scanner s = new Scanner (System.in);
		Scanner t = new Scanner (System.in);
		t.useDelimiter("\\n"); // Prevent scanner from reading code after nextLine().
	
		Barbarian a = new Barbarian ();
		
		System.out.println ("D&D Barbarian Character Generator");
	 
	/*	
		// User Prompts!!! Lots of them.
		System.out.print ("\nPlease enter your character's name: ");
		a.name = s.next();
		
		System.out.print ("\nPlease enter your Barbarian's alignment: ");
		a.alignment = s.next();
		
		System.out.print ("\nPlease enter your character's gender: ");
		a.gender = s.next();
		
		System.out.print ("\nPlease enter your character's height (cm): ");
		a.height = s.nextDouble();
		
		System.out.print ("\nPlease enter your character's weight (lbs): ");
		a.weight = s.nextDouble();
		
		System.out.print ("\nPlease enter your character's religion: ");
		a.religion = s.next();
		
		System.out.print ("\nPlease enter a physical description: ");
		a.description = t.nextLine();
	
		
		// Print character's info. 
		System.out.println ("\n" + a.name + ", a " + a.description + " barbarian, stats: ");
	*/	
		
		// Print out stat names and stats themselves. 
		for (int i = 0; i < 6; i ++){
		System.out.println (a.Statnames()[i] + a.Stats()[i]);		
		}
		
		// Create an array of randomly generated objects 
		Random g = new Random ();
		String[] items = new String[10];
		items[0] = "Rusty sword";
		items[1] = "Thor's hammer";
		items[2] = "Counterfeit money";
		items[3] = "Rusty shield";
		items[4] = "Broadsword";
		items[5] = "Blunt katana";
		items[6] = "Faded leather helmet";
		items[7] = "Torn blue blankie";
		items[8] = "Rotting dead pony";
		items[9] = "Flimsy bow";
		
		int randomnum = g.nextInt(7) + 1;
		
		System.out.println ("Possessions on Person: ");
		
		// Randomly pick number of items that person possesses and output equipped items. 
		for (int i = 0; i < randomnum; i++){
		int index = g.nextInt(items.length);
		if (i < (randomnum - 1)){
			// test
			System.out.print (index);
			System.out.print (items[index] + ", ");
			}
		else{
			System.out.print (index);
			System.out.print (items[index] + ". ");
		}
		}

	}
		
}



I have this program that is attempting to randomly equip the player with items that are randomly chosen from a list. I don't know how to prevent it from picking the same item twice though. I also don't know how to display the items that haven't been equipped, which is another requirement of this program.

Is This A Good Question/Topic? 0
  • +

Replies To: How to prevent randomizer from picking same index in an array twice

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14088
  • View blog
  • Posts: 56,440
  • Joined: 12-June 08

Re: How to prevent randomizer from picking same index in an array twice

Posted 29 April 2015 - 12:42 PM

Either pull the item out of the collection once it is used, or keep a list of item numbers used. The latter would just need a check to say 'hey.. is this number in here? if so pick again'.
Was This Post Helpful? 1
  • +
  • -

#3 selereth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-April 15

Re: How to prevent randomizer from picking same index in an array twice

Posted 30 April 2015 - 11:38 AM

View Postmodi123_1, on 29 April 2015 - 12:42 PM, said:

Either pull the item out of the collection once it is used, or keep a list of item numbers used. The latter would just need a check to say 'hey.. is this number in here? if so pick again'.


Ok.. but since I'm using an array I can't actually remove the index completely, right? Is there a way to simply nullify index so that it's not chosen again or do I have to use something else entirely, like an array list, in order to remove already chosen items?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14088
  • View blog
  • Posts: 56,440
  • Joined: 12-June 08

Re: How to prevent randomizer from picking same index in an array twice

Posted 30 April 2015 - 11:40 AM

No, you cannot cherry pick the values Random produces.

You can always make the array smaller.

Typically I would use a list collection.
Was This Post Helpful? 0
  • +
  • -

#5 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 90
  • View blog
  • Posts: 513
  • Joined: 29-July 09

Re: How to prevent randomizer from picking same index in an array twice

Posted 30 April 2015 - 12:27 PM

Depends what the array is of, if its an array of objects you can make the entry null and if a null is selected in the array , then pick again. Likewise, a similar approach can be applied to arrays of primitives, just select a value which is your null.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: How to prevent randomizer from picking same index in an array twice

Posted 30 April 2015 - 01:26 PM

Using a List is simpler in every way

If you want to use an array, then I suggest you keep the same array length at all times (simpler)
- Keep track of how many items are left that have not been picked yet
- Move picked items to the end of the array
You then have split the array in two parts: not yet picked items, and picked items

A quick overview of what it could look like in code
private class ItemRandomizer {
	//Array of items. Picked items will be swapped to the end of the array
	private String[] items = ...;
	
	//Keep track of how many items that have NOT been picked yet
	private int itemsLeft = ...;

	public String getRandomItem() {
		int index = getRandomIndex();
		String obj = getItemAt(index);
		
		//We don't want to pick the same object again
		removeIndex(index);
	}

	private void removeIndex(int index) {
		//We keep track of how many elements are left in the array
		itemsLeft -= 1;
		
		//Swap the index to the last position of the array
		swap(index, itemsLeft);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1