Jump to content
TomC

Prevent Duplicating Characters In Random String

Recommended Posts

With a bit of help from this forum, I now have a JS generating a random string of characters, of varying length. However, I now realise that for my puposes, I dont want to duplicate any characters within the string, ie I want all characteres to be different.I realise that avoiding duplicating the entire string is an"old chestnut" but is there any way to solve my problem? I think I can see a method using a number of if / else statements, by creating single characters and reexecuting in the event of duplication, but this would be truly horrible.The script I'm using is:function randomString() { var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZ"; var string_length = 1; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } return randomstring;}Grateful for any tips,

Share this post


Link to post
Share on other sites

here's my crude effort

<script>	var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";	var randomstring = '';	var maxLength = 12;	// generate array of random numbers without dups up to the maxLength	var i = 0;	var ranNums = new Array();	var exists = new Boolean();	while (i < maxLength)	{		exists = false;		rnum = Math.floor(Math.random() * chars.length);		for (j=0;j<ranNums.length;j++)		{			if (ranNums[j] == rnum)			{				exists = true;				break;			}		}		if (exists == false)		{			ranNums[i] = rnum;			i++;		}	}	for (j=0;j<maxLength;j++)	{		randomstring += chars.substr(ranNums[j],1);	}	document.write(randomstring);</script>

Note that your original chars string has two 'T's. Drove me crazy

Edited by aalbetski

Share this post


Link to post
Share on other sites

Here's another version:

function randomString() {  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  var string_length = 8;  var randomstring = '';  var rnum, ch;  while (randomstring.length < string_length)  {	rnum = Math.floor(Math.random() * chars.length);	ch = chars.substring(rnum,rnum+1);	if (randomstring.indexOf(ch) == -1)	  randomstring += ch;  }  return randomstring;}

Share this post


Link to post
Share on other sites

The only thing I'd add to both of the examples provided is if the maxLength (or string_length) is variable (e.g. subject to user input) to run a check to make sure that your max length is less than or equal to the length of the chars string. If the chars string was 26 characters long and maxLength was set to 27, you'd get yourself in an infinite loop.

Share this post


Link to post
Share on other sites

Hi All - sorry for slow response.aalbetski - your script a while lot less crude then my effort. And sorry about the two T's in the original list! I had been mucking about with it and left extra char in.justsomeguy - thanks, this looks neat and works well, though to be honest i cant figure out how it works!jesh - good point. In fact I will be end user, but makes sense to ensure char list is > string lengththanks all

Share this post


Link to post
Share on other sites

for the "varying length of characters" change justsomeguys code:from: var string_length = 8;to: var string_length =Math.ceil(Math.random() * chars.length);this will generate value from 1 to 26or to var string_length = Math.floor((26-7)*Math.random()) + 8;this will generate value from 8 to 26(7= value = 1 less than lowest value(8))

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...