[Seaside] Generating a random string

Sven Van Caekenberghe sven at beta9.be
Tue Nov 29 14:55:06 UTC 2011


[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: seaside-bounces at lists.squeakfoundation.org
> [mailto:seaside-bounces at lists.squeakfoundation.org] On Behalf Of Aeren
> Sent: Tuesday, November 29, 2011 9:44 AM
> To: seaside at lists.squeakfoundation.org
> 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
> seaside at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
> _______________________________________________
> seaside mailing list
> seaside at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



More information about the seaside mailing list