How can I generate random integers in a range in Smalltalk?

logical-operators, random, smalltalk

Solution

The problem is that

 (expr) and: (expr) ifTrue: aBlock

is parsed as the method `and:ifTrue:` If you look at the Boolean class (and either True or False in particular), you notice that ifTrue: is just a regular method, and that no method and:ifTrue: exists - however, plain and: does. So to make it clear that these are two messages, write

((expr) and: (expr)) ifTrue: aBlock

For longer boolean combinations, notice that there are also methods and:and: and and:and:and: implemented.

Problem

A class I am taking currently requires us to do all of our coding in smalltalk (it's a Design class). On one of our projects, I am looking to do some things, and am having a tough time finding how to do them. It seems that what most people do is modify their own version of smalltalk to do what they need it to do. I am not at liberty to do this, as this would cause an error on my prof's computer when he doesn't have the same built-in methods I do. Here's what I'm looking to do: Random Numbers. I need to create a random number between 1 and 1000. Right now I'm faking it by doing ``` rand := Random new. rand := (rand nextValue) * 1000. rand := rand asInteger. ``` This gives me a number between 0 and 1000. Is there a way to do this in one command? similar to ``` Random between: 0 and: 1000 ``` And/Or statements. This one bugs the living daylights out of me. I have tried several different configurations of ``` (statement) and: (statement) ifTrue... (statement) and (statement) ifTrue... ``` So I'm faking it with nested ifTrue statements: ``` (statement) ifTrue:[ (statement) ifTrue:[... ``` What is the correct way to do and/or and Random in smalltalk?

Original source