I'm trying to generate a random string, and i've created a method to do so based on a few things I found on internet. However, when I try to generate the string, I receive the error : "Improper store into indexable object". The error comes when I try to insert a new random character into my string. here is my code.
randomString: aSize | alphabet word | alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. word := String new: aSize. 1 to: aSize do: [ :position | word at: position put: [ alphabet at: ( Random between: 1 and: (alphabet size) ) ] ]. ^word Any help? |
You don't want the brackets around
[ alphabet at: ( Random between: 1 and: (alphabet size) ) ] What you wrote tries to store a block closure into a string. Use parentheses instead. Cheers, Bob On 11/29/11 9:43 AM, Aeren wrote: | alphabet word | alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. word := String new: aSize. 1 to: aSize do: [ :position | word at: position put: [ alphabet at: ( Random between: 1 and: (alphabet size) ) ] ]. _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Aeren
[VisualWorks]
alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. size := 32. random := Random new. String streamContents: [:ws | size timesRepeat: [ws nextPut: (alphabet atRandom: random)]]. 'H3H1WNPPEMCNKGHSXK356TA2CA298VNY' HTH, -Boris -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Aeren Sent: Tuesday, November 29, 2011 9:44 AM To: [hidden email] Subject: [Seaside] Generating a random string I'm trying to generate a random string, and i've created a method to do so based on a few things I found on internet. However, when I try to generate the string, I receive the error : "Improper store into indexable object". The error comes when I try to insert a new random character into my string. here is my code. *randomString:* aSize | alphabet word | alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. word := String new: aSize. 1 to: aSize do: [ :position | word at: position put: [ alphabet at: ( Random between: 1 and: (alphabet size) ) ] ]. ^word Any help? -- View this message in context: http://forum.world.st/Generating-a-random-string-tp4119392p4119392.html Sent from the Seaside General mailing list archive at Nabble.com. _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
[Pharo]
| size | size := 32. String streamContents: [ :stream | size timesRepeat: [ stream nextPut: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ atRandom ] ] 'TSBY6AHHXF5YYJDNLOFE7P8C4V2OMA8P' This is a bit slower (due to internal locking), but less code. Sven On 29 Nov 2011, at 15:49, Boris Popov, DeepCove Labs wrote: > [VisualWorks] > > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > size := 32. > random := Random new. > String streamContents: [:ws | size timesRepeat: [ws nextPut: (alphabet > atRandom: random)]]. > > 'H3H1WNPPEMCNKGHSXK356TA2CA298VNY' > > HTH, > > -Boris > > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Aeren > Sent: Tuesday, November 29, 2011 9:44 AM > To: [hidden email] > Subject: [Seaside] Generating a random string > > I'm trying to generate a random string, and i've created a method to do > so based on a few things I found on internet. However, when I try to > generate the string, I receive the error : "Improper store into > indexable object". > The error comes when I try to insert a new random character into my > string. > here is my code. > > *randomString:* aSize > | alphabet word | > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > word := String new: aSize. > 1 to: aSize do: [ :position | > word at: position put: [ alphabet at: ( Random between: > 1 and: (alphabet > size) ) ] ]. > > ^word > > Any help? > > -- > View this message in context: > http://forum.world.st/Generating-a-random-string-tp4119392p4119392.html > Sent from the Seaside General mailing list archive at Nabble.com. > _______________________________________________ > seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside > _______________________________________________ > seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Sven,
Someone ended up writing the below for VisualWorks' version of #atRandom, so I skipped using it in an example, atRandom ^self atRandom: Random new -Boris -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Sven Van Caekenberghe Sent: Tuesday, November 29, 2011 9:55 AM To: Seaside - general discussion Subject: Re: [Seaside] Generating a random string [Pharo] | size | size := 32. String streamContents: [ :stream | size timesRepeat: [ stream nextPut: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ atRandom ] ] 'TSBY6AHHXF5YYJDNLOFE7P8C4V2OMA8P' This is a bit slower (due to internal locking), but less code. Sven On 29 Nov 2011, at 15:49, Boris Popov, DeepCove Labs wrote: > [VisualWorks] > > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > size := 32. > random := Random new. > String streamContents: [:ws | size timesRepeat: [ws nextPut: (alphabet > atRandom: random)]]. > > 'H3H1WNPPEMCNKGHSXK356TA2CA298VNY' > > HTH, > > -Boris > > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Aeren > Sent: Tuesday, November 29, 2011 9:44 AM > To: [hidden email] > Subject: [Seaside] Generating a random string > > I'm trying to generate a random string, and i've created a method to > do so based on a few things I found on internet. However, when I try > to generate the string, I receive the error : "Improper store into > indexable object". > The error comes when I try to insert a new random character into my > string. > here is my code. > > *randomString:* aSize > | alphabet word | > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > word := String new: aSize. > 1 to: aSize do: [ :position | > word at: position put: [ alphabet at: ( Random between: > 1 and: (alphabet > size) ) ] ]. > > ^word > > Any help? > > -- > View this message in context: > http://forum.world.st/Generating-a-random-string-tp4119392p4119392.htm > l Sent from the Seaside General mailing list archive at Nabble.com. > _______________________________________________ > seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside > _______________________________________________ > seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Sven Van Caekenberghe
Well, less code might be
| alphabet aSize | aSize := 20. alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. (String new: aSize) collect: [ :x | alphabet atRandom]. Cheers, Bob On 11/29/11 9:55 AM, Sven Van Caekenberghe wrote: [Pharo] | size | size := 32. String streamContents: [ :stream | size timesRepeat: [ stream nextPut: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ atRandom ] ] 'TSBY6AHHXF5YYJDNLOFE7P8C4V2OMA8P' This is a bit slower (due to internal locking), but less code. Sven On 29 Nov 2011, at 15:49, Boris Popov, DeepCove Labs wrote:[VisualWorks] alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. size := 32. random := Random new. String streamContents: [:ws | size timesRepeat: [ws nextPut: (alphabet atRandom: random)]]. 'H3H1WNPPEMCNKGHSXK356TA2CA298VNY' HTH, -Boris -----Original Message----- From: [hidden email] [[hidden email]] On Behalf Of Aeren Sent: Tuesday, November 29, 2011 9:44 AM To: [hidden email] Subject: [Seaside] Generating a random string I'm trying to generate a random string, and i've created a method to do so based on a few things I found on internet. However, when I try to generate the string, I receive the error : "Improper store into indexable object". The error comes when I try to insert a new random character into my string. here is my code. *randomString:* aSize | alphabet word | alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. word := String new: aSize. 1 to: aSize do: [ :position | word at: position put: [ alphabet at: ( Random between: 1 and: (alphabet size) ) ] ]. ^word Any help? -- View this message in context: http://forum.world.st/Generating-a-random-string-tp4119392p4119392.html Sent from the Seaside General mailing list archive at Nabble.com. _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside_______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Thanks Bob, it works perfectly :)
|
In reply to this post by Aeren
Hi Aeren,
You have two problems with your code. The first looks like a typo. The "[" after put: should be a "(" and its matching "]" should be a ")". As is, the code is trying to store a block where a character should go. The second problem is with your use of Random. There is no #between:and: class or instance method of Random. You need to create a new instance of Random, something like: random := Random seed: 100000. You should look up what a good value for the seed should be, I just used 100000 as an example. Then you should use: (random nextInt: (alphabet size)) to get your next random character. I hope this helps and good luck. Lou >I'm trying to generate a random string, and i've created a method to do so >based on a few things I found on internet. However, when I try to generate >the string, I receive the error : "Improper store into indexable object". >The error comes when I try to insert a new random character into my string. >here is my code. > >*randomString:* aSize > | alphabet word | > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > word := String new: aSize. > 1 to: aSize do: [ :position | > word at: position put: [ alphabet at: ( Random between: 1 and: (alphabet >size) ) ] ]. > > ^word > >Any help? Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon mailto:[hidden email] http://www.Keystone-Software.com _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
Hey Boris,
On 29 Nov 2011, at 15:57, Boris Popov, DeepCove Labs wrote: > Someone ended up writing the below for VisualWorks' version of > #atRandom, so I skipped using it in an example, > > atRandom > ^self atRandom: Random new And I thought that VW was such a fast & efficient implementation ? ;-) Anyway, this would be faster (a dedicated, reused Random object, a preallocated String): | size random alphabet | size := 32. random := Random new. alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. String new: 32 streamContents: [ :stream | size timesRepeat: [ stream nextPut: (alphabet atRandom: random) ] ] Sven_______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Am 29.11.2011 um 16:30 schrieb Sven Van Caekenberghe: > Hey Boris, > > On 29 Nov 2011, at 15:57, Boris Popov, DeepCove Labs wrote: > >> Someone ended up writing the below for VisualWorks' version of >> #atRandom, so I skipped using it in an example, >> >> atRandom >> ^self atRandom: Random new > > And I thought that VW was such a fast & efficient implementation ? > > ;-) > > Anyway, this would be faster (a dedicated, reused Random object, a preallocated String): > > | size random alphabet | > size := 32. > random := Random new. > alphabet := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'. > String new: 32 streamContents: [ :stream | > size timesRepeat: [ > stream nextPut: (alphabet atRandom: random) ] ] > alphabet := Character alphabet asUppercase, ($0 to: $9) Couldn't resist :) Norbert _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
On 29 Nov 2011, at 17:34, Norbert Hartl wrote: > how about changing alphabet to > > alphabet := Character alphabet asUppercase, ($0 to: $9) > > Couldn't resist :) Very nice, I didn't know about #alphabet. You see, these kind of silly conversations can be interesting! Sven _______________________________________________ seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Free forum by Nabble | Edit this page |