shuffle string with optional shuffledness parameter

Share your advanced PureBasic knowledge/code with the community.
rory-games
User
User
Posts: 35
Joined: Sun Feb 02, 2020 9:14 am
Contact:

shuffle string with optional shuffledness parameter

Post by rory-games »

you can shuffle a string!
And the shuffledness parameter will make it more or less shuffled.

Code: Select all

;-------
;shuffle string procedure with optional shuffledness parameter by rb studios
Procedure.s shufflestring(string.s,shuffledness=50)
finalstring.s=""
Dim chars.s(Len(string)-1)
For i = 0 To Len(string)-1
chars(i)=Mid(string,i+1,1)
Next
For i = 1 To Len(string)*shuffledness
Swap chars(Random(Len(string)-1)),chars(Random(Len(string)-1))
Next
For i = 0 To Len(string)-1
finalstring+chars(i)
Next
ProcedureReturn finalstring
EndProcedure
MessageRequester("shuffle test",shufflestring("shuffle em around oh yeah"))
Enjoy!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: shuffle string with optional shuffledness parameter

Post by IdeasVacuum »

Nice, but it shuffles too fast! How about displaying the original string and showing that being shuffled char by char?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply