Page 1 of 2
MakeRandomString
Posted: Sat Nov 22, 2014 2:49 am
by Mistrel
Just a little something I whipped up while testing some code. It's not particularly efficient and has lots of room for improvement, for those who are bored.
Code: Select all
Procedure.s MakeRandomString(Length)
If Not Length
ProcedureReturn ""
EndIf
For i=1 To Length
Number=0
If Random(1,0)
Number=#True
EndIf
If Number
Char.s=Str(Random(9,0))
Else
Char.s=Chr(Random(90,65))
EndIf
RandomString.s+Char.s
Next i
ProcedureReturn RandomString.s
EndProcedure
Re: MakeRandomString
Posted: Sat Nov 22, 2014 4:46 am
by PB
> lots of room for improvement
Here's an optimized version to remove all redundancy:
Code: Select all
Procedure.s MakeRandomString(Length)
For i=1 To Length
If Random(1,0)
RandomString.s+Str(Random(9,0))
Else
RandomString.s+Chr(Random(90,65))
EndIf
Next
ProcedureReturn RandomString.s
EndProcedure
Re: MakeRandomString
Posted: Sat Nov 22, 2014 11:22 am
by infratec
Hi,
since String + String needs a lot of time:
Code: Select all
Procedure.s MakeRandomString(Length)
Protected RandomString.s, *Buffer, i.i
*Buffer = AllocateMemory((Length + 1) * SizeOf(Character))
If *Buffer
Length - 1
For i = 0 To Length
If Random(1, 0)
PokeC(*Buffer + i * SizeOf(Character), Random('9', '0'))
Else
PokeC(*Buffer + i * SizeOf(Character), Random('Z', 'A'))
EndIf
Next
RandomString = PeekS(*Buffer)
FreeMemory(*Buffer)
EndIf
ProcedureReturn RandomString
EndProcedure
Code: Select all
StartTime = ElapsedMilliseconds()
Test$ = MakeRandomString(100000)
EndTime = ElapsedMilliseconds()
MessageRequester("Info", "Need " + Str(EndTime - StartTime) + "ms")
Bernd
Re: MakeRandomString
Posted: Sat Nov 22, 2014 11:56 am
by Fred
For such, a dynamic buffer isn't needed:
Code: Select all
Procedure.s MakeRandomString(Length)
RandomString.s = Space(Length)
Length - 1
For i = 0 To Length
If Random(1, 0)
PokeC(@RandomString + i * SizeOf(Character), Random('9', '0'))
Else
PokeC(@RandomString + i * SizeOf(Character), Random('Z', 'A'))
EndIf
Next
ProcedureReturn RandomString
EndProcedure
Re: MakeRandomString
Posted: Sat Nov 22, 2014 12:56 pm
by Mistrel
Still faster.
Code: Select all
Structure Char
c.c[0]
EndStructure
Procedure.s MakeRandomString(Length)
Protected RandomString.s
Protected Random
Protected *RandomString.Char
Protected i
RandomString.s=Space(Length)
*RandomString=@RandomString.s
Length-1
For i=0 To Length
Random=Random(25,0)
If Random%2
*RandomString\c[i]=65+Random
Else
*RandomString\c[i]=48+Random%10
EndIf
Next
ProcedureReturn RandomString.s
EndProcedure
Debug MakeRandomString(0)
Re: MakeRandomString
Posted: Sat Nov 22, 2014 1:02 pm
by STARGÅTE
This is no uniform distribution, because you get more numbers between 0 and 5 than numbers between 6 and 9.
I use this RandomString:
Code: Select all
CompilerIf Defined(CharacterArray, #PB_Structure) = #False
Structure CharacterArray
c.c[0]
EndStructure
CompilerEndIf
#RandomString_Numeric = "0123456789"
#RandomString_Letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#RandomString_Serial = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Procedure.s RandomString(Length.i, CharacterSet.s=#RandomString_Letter)
Protected Index.i, Len.i = Len(CharacterSet)-1
Protected *CharacterSet.CharacterArray = @CharacterSet
Protected String.s = Space(Length)
Protected *String.CharacterArray = @String
While Index < Length
*String\c[Index] = *CharacterSet\c[Random(Len)]
Index + 1
Wend
ProcedureReturn String
EndProcedure
Debug RandomString(16)
Debug RandomString(16, #RandomString_Numeric)
Debug RandomString(16, #RandomString_Serial)
Re: MakeRandomString
Posted: Sat Nov 22, 2014 1:10 pm
by Mistrel
STARGÅTE wrote:
This is no uniform distribution, because you get more numbers between 0 and 5 than numbers between 6 and 9.
You could throw another random in there if you want it to be more distributed, sure.
I didn't run any tests but just from observation I think yours might be faster while also being uniform; although it does have a call to Len() which you could store somewhere.
Re: MakeRandomString
Posted: Sat Nov 22, 2014 2:37 pm
by PB
> you get more numbers between 0 and 5 than numbers between 6 and 9
Which is still perfectly random, you know.

By the very definition of "random",
you don't get to "prefer" which numbers get picked more often than any others.
Re: MakeRandomString
Posted: Sat Nov 22, 2014 11:52 pm
by Mistrel
Wouldn't it be considered pseudorandom if an uneven distribution can be proven?
Re: MakeRandomString
Posted: Sun Nov 23, 2014 12:30 am
by PB
> uneven distribution can be proven
That's a scientific impossibility when it comes to randomness.
Again, you're thinking that randomness MUST have an even
distribution. Random means random: you NEVER know what
the results will be, and there must NEVER be an expectation
or preference of the results.
Think of it like this: just because a coin is flipped 100 times and
99 of those came up heads, doesn't mean it wasn't random.

It's just human nature bitching about the results because they
weren't the results we expected. But that's the very definition
of randomness!

Re: MakeRandomString
Posted: Sun Nov 23, 2014 12:34 am
by Mistrel
Yes, but given a sufficient sample size you can determine probability.
Re: MakeRandomString
Posted: Sun Nov 23, 2014 12:38 am
by PB
If you can determine probability then it's not random.

Re: MakeRandomString
Posted: Sun Nov 23, 2014 1:12 am
by Mistrel
That's what I'm saying.
If it's an uneven distribution then you can calculate probability.
Re: MakeRandomString
Posted: Sun Nov 23, 2014 1:15 am
by PB
I misunderstood you.

Re: MakeRandomString
Posted: Sun Nov 23, 2014 3:11 am
by skywalk
Even the slightest bias in a game of chance can result in serious losses for online and brick and mortar casinos.
99 out of a 100 flips would break any casino.
