Generate a secret code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 3825 Views - Last Post: 19 April 2009 - 03:30 AM Rate Topic: -----

#1 Rita123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-April 09

Generate a secret code

Post icon  Posted 18 April 2009 - 06:37 PM

,,

This post has been edited by Rita123: 19 April 2009 - 03:59 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Generate a secret code

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Generate a secret code

Posted 18 April 2009 - 06:53 PM

you know, a good idea for this is to generate all the permutations of "ABCDEF"

This post has been edited by mostyfriedman: 18 April 2009 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 Rita123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-April 09

Re: Generate a secret code

Posted 18 April 2009 - 07:00 PM

View Postmostyfriedman, on 18 Apr, 2009 - 05:53 PM, said:

you know, a good idea for this is to generate all the permutations of "ABCDEF"


what do you mean? i didn't get it :wacko:
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Generate a secret code

Posted 18 April 2009 - 07:09 PM

permutations are all the possible arrangements of certain letters example:
abc
bac
bca
cba
cab
etc...

This post has been edited by mostyfriedman: 18 April 2009 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 Rita123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-April 09

Re: Generate a secret code

Posted 18 April 2009 - 07:37 PM

,,

This post has been edited by Rita123: 19 April 2009 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Generate a secret code

Posted 18 April 2009 - 07:57 PM

if you generate the permutations then you are generating all possible secret words that wont have repitition
Was This Post Helpful? 0
  • +
  • -

#7 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Generate a secret code

Posted 18 April 2009 - 11:07 PM

will something like this work

private String generateSecretCode() {
		String[] letters = {"A","B","C","D","E","F"};

		int first = (int)(Math.random() * 6);
		char firstLetter = letters[first];
		letter[first] = null;

		
		int second = (int)(Math.random() * 6);
		while(letters[second]==null)
			  int second = (int)(Math.random() * 6);
		char secondLetter = letters[second];
				letter[second] = null;

		int third = (int)(Math.random() * 6);
		while(letters[third]==null)
				int third = (int)(Math.random() * 6);
		char thirdLetter = letters[third];
				letter[third] = null;
		
		int fourth = (int)(Math.random() * 6);
		while(letters[fourth]==null)
				int fourth = (int)(Math.random() * 6);
		char fourthLetter = letters[fourth];
				letter[fourth] = null; 

		String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
		return secretCode;


This post has been edited by virgul: 18 April 2009 - 11:09 PM

Was This Post Helpful? 1
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Generate a secret code

Posted 18 April 2009 - 11:19 PM

Based off of the fact that the word in context is only allowed to be 4 letters long. This should work fine
int length =4;
Random r = new Random();
chars[] letters = {'A', 'B', 'C', 'D', 'E' };
String word = "";
for(int i = 0; i< length; i++)
	   word += letters[r.nextInt(letters.length)];
return word;


Short, simple, to the point. Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#9 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Generate a secret code

Posted 18 April 2009 - 11:44 PM

fuzzy only problem is repeating, yours has a very high probability of repeating.
Was This Post Helpful? 0
  • +
  • -

#10 Rita123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-April 09

Re: Generate a secret code

Posted 18 April 2009 - 11:54 PM

,,

This post has been edited by Rita123: 19 April 2009 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Generate a secret code

Posted 18 April 2009 - 11:54 PM

Aah, I see didnt see the no repeated thing in first post. *sigh*
Oooook......
int length =4;
int index = 0;
Random r = new Random();
chars[] letters = {'A', 'B', 'C', 'D', 'E' };
String word = "";
for(int i = 0; i< length; i++)
{
index = r.nextInt(letters.length);
while(letters[index] == null)
		 {index = r.nextInt(letters.length);}
word += letters[index];
letters.set(index , '');
}
return word;


Sets the index it used to null, and if the generator selects that char, then it will keep going untl it selects one that isnt null, or already been used. Thus getting rid of the repetitive. COuld make it anything, could reset it to '-' and if the char at that index equals '-' then it keeps going. Pretty sure this should work

Edit - dont forget to import Random class and Arrays class

This post has been edited by Fuzzyness: 18 April 2009 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#12 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Generate a secret code

Posted 19 April 2009 - 12:08 AM

Rita, as for your errors, are they errors, or Warnings

and Fuzzy, it looks like mine =) happy to see my way can work!!!
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Generate a secret code

Posted 19 April 2009 - 12:11 AM

Mine avoids magic numbers if you assign length to user input ;)
and didn't even notice it was so much like yours lol, Sorry wasn't trying copy =/
Was This Post Helpful? 0
  • +
  • -

#14 Rita123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-April 09

Re: Generate a secret code

Posted 19 April 2009 - 12:13 AM

..

This post has been edited by Rita123: 19 April 2009 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Generate a secret code

Posted 19 April 2009 - 12:18 AM

its a simple single code.
import java.util.Random;
Goes outside of the class. each dot represent a branch in the API. We use Java, so duh.
In the java we want the util, so its java.util
then we want the Random class in that package, so java.util.Random
Some statements may go further then that for class can have sub classes.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3