Useful string jumbler function

This is a discussion on "Useful string jumbler function" within the Classic ASP section. This forum, and the thread "Useful string jumbler function are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Nov 6th, 2003, 11:18
Rob's Avatar
Rob Rob is online now
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,188
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
Useful string jumbler function

Someone wanted a function to take a comma seperated string and jumble the items around into a different order. I thought I would share this all with you incase it would become handy.

I knocked this up in a few minutes, so apologies if anything is bugged.

Code: Select all
Function jumbleStringArray(strItems, strSeperator)
	Dim TempFlag1, TempFlag2, TempStr, ItemArray
	ItemArray = Split(StrItems,strSeperator)
	For Counter = 0 to (ubound(ItemArray)) * 5
		TempFlag2 = 999999999
		Randomize()
		TempFlag1 = int((ubound(ItemArray)+1)*rnd())
		do
			Randomize
			TempFlag2 = int((ubound(ItemArray)+1)*rnd())
		loop Until TempFlag2 <> TempFlag1
		TempStr = ItemArray(TempFlag1)
		ItemArray(TempFlag1) = Trim(ItemArray(TempFlag2))
		ItemArray(TempFlag2) = Trim(TempStr)
	Next
	TempStr=""
	For Counter = 0 to ubound(ItemArray)
		TempStr = TempStr & ItemArray(Counter) & ","
	Next
	TempStr = left(TempStr,len(TempStr)-1)
	jumbleStringArray = TempStr
End Function
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Nov 10th, 2003, 11:17
Highly Reputable Member
Join Date: Jul 2003
Location: Ipswich, UK
Posts: 690
Thanks: 0
Thanked 0 Times in 0 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Nov 18th, 2003, 14:29
Junior Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Cool code. A few points (I'm being rather picky here):
- The last 6 lines can be replaced with jumbleStringArray = Join(ItemArray(Counter), ",")
- Why Randomize so much? Just one at the start of the function does exactly the same.
- The tempflag2 = 999999999 isn't needed.
- Why the do-loop bit in the middle? If it's random then it should allow a swap of index 3 with index 3, for example.
- I'd make the * 5 actually based on the size of the array - doing 5 loops wouldn't be sufficient on a large array.
- Doing it this way obviously wouldn't be wholey random - there are much slicker solutions for properly randomizing.

Here's mine, it's not optimised (the keep-looping-till-we-get-an-unused-index bit is rather crappy) but it seems to run fast enough. Should be perfectly random:

Code: Select all
Option Explicit
Function JumbleStringArray(strItems, strSeperator)
	Randomize()
	Dim aryStart, intArraySize, aryResult, aryUsedIndexes, intCounter, intRndIndex, i
	aryStart = split(strItems, strSeperator)
	intArraySize = ubound(aryStart)
	ReDim aryResult(intArraySize)
	Redim aryUsedIndexes(intArraySize)
	For i = intCounter to intArraySize
		Do
			intRndIndex = int(rnd() * (intArraySize + 1))
		Loop Until aryUsedIndexes(intRndIndex) = ""
		aryResult(intRndIndex) = aryStart(i)
		aryUsedIndexes(intRndIndex) = "y"		
	Next
	JumbleStringArray = Join(aryResult, strSeperator)
End Function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Nov 20th, 2003, 10:01
Highly Reputable Member
Join Date: Jul 2003
Location: Ipswich, UK
Posts: 690
Thanks: 0
Thanked 0 Times in 0 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Nov 20th, 2003, 12:22
Rob's Avatar
Rob Rob is online now
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,188
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
Hi... <blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">The tempflag2 = 999999999 isn't needed.
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">I actually used a do while loop initially, so I guess I forgot to take this out.<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">Why the do-loop bit in the middle? If it's random then it should allow a swap of index 3 with index 3, for example.<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">Just my personal preference, thats all.<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">I'd make the * 5 actually based on the size of the array - doing 5 loops wouldn't be sufficient on a large array.<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">If you look again, it IS based on the size of the array.... 5 * the number of elements in the array to be precise.<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">Why Randomize so much? Just one at the start of the function does exactly the same.<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">LOL... that was supposed to be outside of the for loop. Well Spotted! (I did say I knocked it up rather quickly)<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">Join(aryResult, strSeperator)<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">Kewl.... never come across that one before. I coulda saved SOOO much time in the past.

And you are right.... yer a picky sod.

Next time I submt some code, I shall endeavour to pick my brains for hours before-hand to make it as good as possible and not open to nit picks like you!! ROFLMAO
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Tags
useful, string, jumbler, function

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get value from SQL string ? VegaLA Classic ASP 5 Jan 30th, 2008 07:39
string capacity Don Logan JavaScript Forum 11 Sep 23rd, 2006 20:28
append % to the string chandra.nowduri ASP.NET Forum 1 Aug 9th, 2006 09:47
Connection String DSN-less gwx03 Classic ASP 11 Nov 26th, 2003 12:07
dns less connection String djaccess Classic ASP 7 Oct 5th, 2003 14:34


All times are GMT. The time now is 13:45.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42