. .

st4u

ST 4U 292: Random Numbers in VA Smalltalk

October 5, 2012 11:35:41.033

Today's Smalltalk 4 You looks at random number generation in VA Smalltalk. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Random Numbers.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll look at random number generation in VA Smalltalk. The class you'll want to look at is EsRandom. The common way to get a random number in Smalltalk is like this:


| rand randVal |
rand := EsRandom new.
randVal := rand next.

That yields a floating point value with a lot of digits to the right of the decimal place. In many Smalltalks, you end up multiplying that by a number (to put it in the range you want), rounding, and adding one. In VA, there's a nice convenience method, #nextInt: - you give it a value, and you get back a random number between 1 and that value:


| rand randVal |
rand := EsRandom new.
randVal := rand nextInt: 100.

Display the results to make sure you get back what you expect - in this case, it should be a value between one and one hundred.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

Enclosures:
[st4u292-iPhone.m4v ( Size: 2244155 )]

posted by James Robertson

 Share Tweet This