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

The class that I am currently taking requires us to do all our coding in smalltalk (this is the Design class). In one of our projects, I want to do something, and it's hard for me to find how to do them. It seems like most people do this to change their own version of smalltalk to do what they need. I am not entitled to do this because it can cause an error on my computer if it does not have the same built-in methods that I do.

Here is what I want to do:

Random numbers. I need to create a random number from 1 to 1000. Right now I'm pretending to do

rand := Random new.
rand := (rand nextValue) * 1000.
rand := rand asInteger.

This gives me a number from 0 to 1000. Is there a way to do this in one command? similarly

Random between: 0 and: 1000

And / or statements. It casts out daylight from me. I tried several different configurations

(statement) and: (statement) ifTrue...
(statement) and (statement) ifTrue...

So, I fake it using nested ifTrue statements:

(statement) ifTrue:[
    (statement) ifTrue:[...

What is the correct way to do and / or random in smalltalk?

+3
source share
6 answers

The problem is

 (expr) and: (expr) ifTrue: aBlock

parsed as a method and:ifTrue:If you look at the Boolean class (and, in particular, True or False), you will notice that ifTrue: is a normal method and that there is no method and: ifTrue: exists - however, it is simple and :. Therefore, to make it clear that these are two messages, write

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

For longer logical combinations, note that there are also methods: and: and: and and: and: and: implemented.

+6
source

VisualWorks : , :

(aBoolean and: [anotherBoolean]) ifTrue: [doSomething].

&, ,

aBoolean & anotherBoolean ifTrue:[doSomething].

: , , bool ( java), & .

, and: , , , . ( , ).

Random, , Random >> between: and:, , . , , .

+5

: , ST . Squeak 3.9 Random>>#nextInt:, " [1, anInteger]".

(self next * anInteger) truncated + 1

, :

  • . " X"
  • ST . , , Random : :, , .

    between: low and: high      
       ^(self next * (high-low+1)) truncated + low
    
+4
(1 to: 1000) atRandom
+4

1 1000
. .

, . .

aRandomSeries := Random new .
    "Seed a new series of random numbers"  

aRandomInt := aRandomSeries newInt: 1000 . 
    "generate a random integer between 0 and 1000"

anotherRandomInt := aRandomSeries newInt: 1000 .
    "generate another random integer between 0 and 1000"

aBoolean and: or:. .

.

and: alternativeBlock
, alternativeBlock; false, .

or: alternativeBlock
, alternativeBlock; true .

.
( 3 > 2 ) or: [ 3 < 4 ] ifTrue: [ ]
aBoolean and: [ anotherBoolean ] ifFalse: [ ]

Squeak Pharo Smalltalks ( )
Dolphin Smalltalk Smalltalk .

:
& a AND, ( )
| OR, ( )
& | Amber, Cuis, Gnu, Pharo, Squeak, VisualAge VisualWorks Smalltalks.

Squeak Smalltalk :
and:and: }
and:and:and: }
and:and:and:and }

or:or: }
or:or:or: }
or:or:or:or: }

0

Simply put, without knowing the Smalltalk dialect, I can only give a general answer. As you stated a random question, yes, that the only way to do this is if your professor needs a general answer.

Regarding the question and / or statement,

And / or statements. It casts out daylight from me. I tried several different configurations

(statement) and: (statement) ifTrue...
(statement) and (statement) ifTrue...

What do you want to try:

(statement) and: [statement] ifTrue: [ ... ]

note the brackets, the and: method takes a block as an argument.

0
source

Source: https://habr.com/ru/post/1699258/


All Articles