如何在Smalltalk的范围内生成随机整数?

时间:2022-11-25 19:37:58

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.

我正在上课的课程要求我们在smalltalk中完成所有编码(这是一个Design类)。在我们的一个项目中,我希望做一些事情,并且很难找到如何做到这一点。似乎大多数人所做的就是修改自己的smalltalk版本来做他们需要做的事情。我不能*地这样做,因为当我没有相同的内置方法时,这会导致我教授的计算机出错。

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

随机数。我需要创建一个1到1000之间的随机数。现在我正在伪装它

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

这给了我一个0到1000之间的数字。有没有办法在一个命令中执行此操作?相近

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:

所以我用嵌套的ifTrue语句伪装它:

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

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

在smalltalk中正确的做法和/或随机是什么?

6 个解决方案

#1


6  

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

被解析的方法和:ifTrue:如果你看一下布尔类(True或特别假),您注意到ifTrue:只是一个普通的方法,而且没有方法:ifTrue:存在 - 然而,平原并且:确实。所以要明确这些是两条消息,写一下

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

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

对于更长的布尔组合,请注意还有方法和:和:和:和:和:和:已实现。

#2


5  

If you're using VisualWorks, and: takes a block as an argument, so you'd write:

如果您正在使用VisualWorks,并且:将块作为参数,那么您将编写:

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

There's also &, which does not take a block as argument,

还有&,它不会作为参数,

aBoolean & anotherBoolean ifTrue:[doSomething].

The difference is and: only evaluates what's in the block if the first bool is true (similar to java), while & always evaluates both.

区别在于:仅在第一个bool为真时(类似于java)计算块中的内容,而&始终对两者进行求值。

Thus and: comes in handy if the second condition is computationally expensive, or if it includes state alterations which should only happen when the first condition is true. (that's usually a bad design though).

因此,如果第二个条件在计算上是昂贵的,或者如果它包括仅在第一个条件为真时才发生的状态改变,则派上用场。 (虽然这通常是一个糟糕的设计)。

As for the Random, as long as you deliver your custom method, Random >> between: and: as well as the rest of your code, it runs fine on your professors computer. How to do that specifically, depends on the format in which you are supposed to deliver the assignment.

对于Random,只要你提供自定义方法,Random >> between:和:以及代码的其余部分,它就可以在你的教授计算机上正常运行。具体如何,取决于您应该交付作业的格式。

#3


4  

As for the Random issue: it depends on what ST version you use. In Squeak 3.9, there is Random>>#nextInt:, which is documented as "Answer a random integer in the interval [1, anInteger].". Its implementation reads

至于随机问题:它取决于你使用的ST版本。在Squeak 3.9中,有Random >>#nextInt:,它被记录为“在区间[1,anInteger]中回答一个随机整数。”。它的实现读取

(self next * anInteger) truncated + 1

So I have two comments here:

所以我在这里有两条评论:

  1. You should really learn to use the class browser. This can answer the (frequent) questions "what messages can I send to objects of class X"
  2. 你应该学会使用类浏览器。这可以回答(常见的)问题“我可以向X类对象发送什么消息”

  3. It is common, in ST, to add new methods to existing classes. So if you want Random to have between:and:, just add it, e.g. as

    在ST中,常见的是向现有类添加新方法。因此,如果您希望Random位于:和:之间,只需添加它,例如如

    between: low and: high      
       ^(self next * (high-low+1)) truncated + low
    

#4


4  

(1 to: 1000) atRandom

#5


0  

To put it simply, without knowing the Smalltalk dialect, I can only give a general answer. The way you stated the random question, yes that's the only way to do it if your professor needs a generic answer.

简单地说,在不知道Smalltalk方言的情况下,我只能给出一般答案。你提出随机问题的方式,是的,如果你的教授需要一个通用的答案,这是唯一的方法。

As for the and/or statements question,

至于和/或陈述问题,

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...

What you want to try is:

你想要尝试的是:

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

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

注意括号,and和:方法将块作为参数。

#6


0  

To create several random integers between 1 and 1000
First create a random number series. Do this just once.

要创建1到1000之间的几个随机整数首先创建一个随机数系列。这样做一次。

Then create a new random number by taking the next number from the series. Repeat as necessary.

然后通过从系列中获取下一个数字来创建一个新的随机数。根据需要重复。

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"

Logical operations

aBoolean will respond to and: and or:. They both take block arguments.

aBoolean将响应和:和或:他们都采取块参数。

Here is how they work.

这是他们的工作方式。

and: alternativeBlock
If the receiver is true, answer the value of alternativeBlock; otherwise answer false without evaluating alternativeBlock.

和:alternativeBlock如果接收方为true,则回答alternativeBlock的值;否则在不评估alternativeBlock的情况下回答false。

or: alternativeBlock
If the receiver is false, answer the value of alternativeBlock; otherwise answer true without evaluating alternativeBlock.

或:alternativeBlock如果接收方为false,则回答alternativeBlock的值;否则在不评估alternativeBlock的情况下回答为true。

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

例如(3> 2)或:[3 <4] ifTrue:[] aBoolean和:[anotherBoolean] ifFalse:[]

However, Squeak and Pharo Smalltalks will both accept an argument in parentheses ( )
Dolphin Smalltalk will not, and strictly requires the standard Smalltalk syntax of a block argument.

但是,Squeak和Pharo Smalltalks都会在括号中接受一个参数()Dolphin Smalltalk不会,并且严格要求使用块参数的标准Smalltalk语法。

Other related methods:
& an AND that does not require a square bracketted (i.e. block) argument
| an OR that does not require a square bracketted (i.e. block) argument
& and | work in Amber, Cuis, Gnu, Pharo, Squeak, VisualAge and VisualWorks Smalltalks.

其他相关方法:&不需要方括号(即块)参数的AND OR不需要方括号(即块)参数&和|在Amber,Cuis,Gnu,Pharo,Squeak,VisualAge和VisualWorks Smalltalks工作。

Squeak Smalltalk also provides:
and:and: }
and:and:and: } These take multiple block arguments
and:and:and:and }

Squeak Smalltalk还提供:and:and:}和:and:and:}它们采用多个块参数和:和:和:和}

or:or: }
or:or:or: } These take multiple block arguments
or:or:or:or: }

或:或:}或:或:或:}这些采用多个块参数或:或:或:或:}

#1


6  

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

被解析的方法和:ifTrue:如果你看一下布尔类(True或特别假),您注意到ifTrue:只是一个普通的方法,而且没有方法:ifTrue:存在 - 然而,平原并且:确实。所以要明确这些是两条消息,写一下

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

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

对于更长的布尔组合,请注意还有方法和:和:和:和:和:和:已实现。

#2


5  

If you're using VisualWorks, and: takes a block as an argument, so you'd write:

如果您正在使用VisualWorks,并且:将块作为参数,那么您将编写:

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

There's also &, which does not take a block as argument,

还有&,它不会作为参数,

aBoolean & anotherBoolean ifTrue:[doSomething].

The difference is and: only evaluates what's in the block if the first bool is true (similar to java), while & always evaluates both.

区别在于:仅在第一个bool为真时(类似于java)计算块中的内容,而&始终对两者进行求值。

Thus and: comes in handy if the second condition is computationally expensive, or if it includes state alterations which should only happen when the first condition is true. (that's usually a bad design though).

因此,如果第二个条件在计算上是昂贵的,或者如果它包括仅在第一个条件为真时才发生的状态改变,则派上用场。 (虽然这通常是一个糟糕的设计)。

As for the Random, as long as you deliver your custom method, Random >> between: and: as well as the rest of your code, it runs fine on your professors computer. How to do that specifically, depends on the format in which you are supposed to deliver the assignment.

对于Random,只要你提供自定义方法,Random >> between:和:以及代码的其余部分,它就可以在你的教授计算机上正常运行。具体如何,取决于您应该交付作业的格式。

#3


4  

As for the Random issue: it depends on what ST version you use. In Squeak 3.9, there is Random>>#nextInt:, which is documented as "Answer a random integer in the interval [1, anInteger].". Its implementation reads

至于随机问题:它取决于你使用的ST版本。在Squeak 3.9中,有Random >>#nextInt:,它被记录为“在区间[1,anInteger]中回答一个随机整数。”。它的实现读取

(self next * anInteger) truncated + 1

So I have two comments here:

所以我在这里有两条评论:

  1. You should really learn to use the class browser. This can answer the (frequent) questions "what messages can I send to objects of class X"
  2. 你应该学会使用类浏览器。这可以回答(常见的)问题“我可以向X类对象发送什么消息”

  3. It is common, in ST, to add new methods to existing classes. So if you want Random to have between:and:, just add it, e.g. as

    在ST中,常见的是向现有类添加新方法。因此,如果您希望Random位于:和:之间,只需添加它,例如如

    between: low and: high      
       ^(self next * (high-low+1)) truncated + low
    

#4


4  

(1 to: 1000) atRandom

#5


0  

To put it simply, without knowing the Smalltalk dialect, I can only give a general answer. The way you stated the random question, yes that's the only way to do it if your professor needs a generic answer.

简单地说,在不知道Smalltalk方言的情况下,我只能给出一般答案。你提出随机问题的方式,是的,如果你的教授需要一个通用的答案,这是唯一的方法。

As for the and/or statements question,

至于和/或陈述问题,

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...

What you want to try is:

你想要尝试的是:

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

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

注意括号,and和:方法将块作为参数。

#6


0  

To create several random integers between 1 and 1000
First create a random number series. Do this just once.

要创建1到1000之间的几个随机整数首先创建一个随机数系列。这样做一次。

Then create a new random number by taking the next number from the series. Repeat as necessary.

然后通过从系列中获取下一个数字来创建一个新的随机数。根据需要重复。

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"

Logical operations

aBoolean will respond to and: and or:. They both take block arguments.

aBoolean将响应和:和或:他们都采取块参数。

Here is how they work.

这是他们的工作方式。

and: alternativeBlock
If the receiver is true, answer the value of alternativeBlock; otherwise answer false without evaluating alternativeBlock.

和:alternativeBlock如果接收方为true,则回答alternativeBlock的值;否则在不评估alternativeBlock的情况下回答false。

or: alternativeBlock
If the receiver is false, answer the value of alternativeBlock; otherwise answer true without evaluating alternativeBlock.

或:alternativeBlock如果接收方为false,则回答alternativeBlock的值;否则在不评估alternativeBlock的情况下回答为true。

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

例如(3> 2)或:[3 <4] ifTrue:[] aBoolean和:[anotherBoolean] ifFalse:[]

However, Squeak and Pharo Smalltalks will both accept an argument in parentheses ( )
Dolphin Smalltalk will not, and strictly requires the standard Smalltalk syntax of a block argument.

但是,Squeak和Pharo Smalltalks都会在括号中接受一个参数()Dolphin Smalltalk不会,并且严格要求使用块参数的标准Smalltalk语法。

Other related methods:
& an AND that does not require a square bracketted (i.e. block) argument
| an OR that does not require a square bracketted (i.e. block) argument
& and | work in Amber, Cuis, Gnu, Pharo, Squeak, VisualAge and VisualWorks Smalltalks.

其他相关方法:&不需要方括号(即块)参数的AND OR不需要方括号(即块)参数&和|在Amber,Cuis,Gnu,Pharo,Squeak,VisualAge和VisualWorks Smalltalks工作。

Squeak Smalltalk also provides:
and:and: }
and:and:and: } These take multiple block arguments
and:and:and:and }

Squeak Smalltalk还提供:and:and:}和:and:and:}它们采用多个块参数和:和:和:和}

or:or: }
or:or:or: } These take multiple block arguments
or:or:or:or: }

或:或:}或:或:或:}这些采用多个块参数或:或:或:或:}