Fwd: Re: random number between 2 numbers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Re: random number between 2 numbers

Roelof



-------- Origineel bericht --------
Onderwerp: Re: [vwnc] random number between 2 numbers
Van: Roelof Wobben [hidden email]
Antwoord-naar: Roelof Wobben [hidden email]
Aan: Donald MacQueen [hidden email]


Op 11 april 2014 om 16:24 schreef Donald MacQueen [hidden email]:


On 4/11/2014 10:10 AM, Roelof Wobben wrote:
> Roelof Wobben schreef op 11-4-2014 13:27:

Roelof,

You generally do not want or need to concern yourself about where in a
set things go. Try this:

| grid |

grid := Set new.

numbers do: [:cijfer | grid add: cijfer ].

or, if numbers is an OrderedCollection, you can do:

| grid |

grid := Set new.

grid addAll: numbers.

hth,

Oke, maybne I need to use another Collection.

I have a grid of 4 x 4 which can contain a number of is empty.

So I thought to use a set to make a model of the grid.


Roelof




_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Nowak, Helge

Look at the Collections class hierarchy to understand the differences and choose the one which suits your needs. See also chapter 1 of the ”Basic Libraries Guide” manual in the “doc” directory of your VisualWorks installation.

 

HTH

Helge

 

Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roelof Wobben
Gesendet: Freitag, 11. April 2014 14:53
An: [hidden email]
Betreff: [vwnc] Fwd: Re: random number between 2 numbers

 

 



-------- Origineel bericht --------

Onderwerp:

Re: [vwnc] random number between 2 numbers

Van:

Roelof Wobben [hidden email]

Antwoord-naar:

Roelof Wobben [hidden email]

Aan:

Donald MacQueen [hidden email]

 

Op 11 april 2014 om 16:24 schreef Donald MacQueen [hidden email]:


On 4/11/2014 10:10 AM, Roelof Wobben wrote:
> Roelof Wobben schreef op 11-4-2014 13:27:

Roelof,

You generally do not want or need to concern yourself about where in a
set things go. Try this:

| grid |

grid := Set new.

numbers do: [:cijfer | grid add: cijfer ].

or, if numbers is an OrderedCollection, you can do:

| grid |

grid := Set new.

grid addAll: numbers.

hth,

Oke, maybne I need to use another Collection.

I have a grid of 4 x 4 which can contain a number of is empty.

So I thought to use a set to make a model of the grid.

 

Roelof

 

 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Nowak, Helge schreef op 11-4-2014 17:16:

Look at the Collections class hierarchy to understand the differences and choose the one which suits your needs. See also chapter 1 of the ”Basic Libraries Guide” manual in the “doc” directory of your VisualWorks installation.

 

HTH

Helge



Thanks,

I have read the document and I doubt between a Directonary or a Array.
I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

Roelof

 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

jWarrior
On 4/11/2014 1:48 PM, Roelof Wobben wrote:

Roelof,

You can use anything you want as a dictionary key.  You could try representing your grid as an Array of Arrays. 

| grid |

grid := Array new.
grid add: (Array with: 1 with: 2).
grid add: (Array with: 3 with: 4).

To retrieve the value 2, inspect (grid first) last.

If you are coming from another language, an excellent start is the book Smalltalk by Example available here: http://stephane.ducasse.free.fr/FreeBooks.html

I am not a VW guy, but they may have a Matrix class.

Keep trying.



Thanks,

I have read the document and I doubt between a Directonary or a Array.
I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

Roelof

 



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


-- 
Donald [|]

Joel Cairo: "You always have a very smooth explanation."
Sam Spade: "What do you want me to do? Learn to stutter?"
- The Maltese Falcon

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Donald MacQueen schreef op 11-4-2014 20:09:
On 4/11/2014 1:48 PM, Roelof Wobben wrote:

Roelof,

You can use anything you want as a dictionary key.  You could try representing your grid as an Array of Arrays. 

| grid |

grid := Array new.
grid add: (Array with: 1 with: 2).
grid add: (Array with: 3 with: 4).

To retrieve the value 2, inspect (grid first) last.

If you are coming from another language, an excellent start is the book Smalltalk by Example available here: http://stephane.ducasse.free.fr/FreeBooks.html

I am not a VW guy, but they may have a Matrix class.

Keep trying.


Thanks,
I will look at that book but at first glance I miss exercises.

What do you experts think of this book :  http://stephane.ducasse.free.fr/FreeBooks/InsideST/InsideSmalltalk.pdf

Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Paul Baumann
In reply to this post by jWarrior

Hi Roelof,

 

A little more detail...

 

>> I have read the document and I doubt between a Directonary or a Array.
>> I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

 

Array can only be accessed with integer "keys", and only within the size allocated. Arrays don't grow in VisualWorks and most other Smalltalk dialects, so if you need growth then use an OrderedCollection instead of an Array. Both OC and Array will error if you attempt to access a position greater than the size allocated or less than 1. Arrays and OC are extremely fast at finding an object for an integer key but you wouldn't want to use them for a sparsely populated collection because a slot is allocated for every possible key. Use a Dictionary unless you are certain that an Array meets your needs.

 

A Dictionary allows "any" kind of object (except nil) to be used as a key. An object that is used as the key of a dictionary is first sent #hash to determine where to start looking for an equal key, keys are then compared with #= equality tests to find a match. If you use your own objects as keys then you sometimes want to define what it means for two of those objects to be considered equal. Equal objects need to return true from #=, but what most people new to Smalltalk miss is that those equal objects need to also answer an equal number from #hash so that hashed collections (like Dictionary and Set for example) start looking for a match at the correct index position. Kernel objects like integer and point (the examples you gave) already define both #= and #hash. Most people use common objects like strings and integers for dictionary keys instead of defining what it means for two of their domain objects to be considered equal.

 

An interval is an object that is similar to the "(1 - 16)...or 1@1" object you seemed to be considering as a key. I suspect you are thinking of using a dictionary different from how it is designed to operate. This demonstrates that key like that is one object and not many:

 

Dictionary new

            at: (1 to: 16) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#findme

 

Dictionary new

            at: (1 to: 1000) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#nothing

 

Here are examples of filling with a range of keys:

 

| index |

index := OrderedCollection new.

1 to: 16 do: [:i | index at: i put: #findme].

index at: 15

 

or:

 

| index |

index := Dictionary new.

1 to: 16 do: [:i | index at: i put: #findme ].

index at: 15

 

Your original question seemed to be more about how to get random numbers within a range. This is an example that uses a block to get 1000 random integers between 33 and 64:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [(randomStream next * range size + range first) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

Or, now reading randomly from values in the range interval:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

And now an example of randomly selecting from a collection of specific values:

 

| range randomStream aBlock |

randomStream := Random new.

range := #(43 61 89 127 181 257 367 521 739 1049 1481 2099).

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

All your "power of 2" numbers can be in the 'range' collection above. You could randomly select any object from a collection in the same way. Figuring out how the examples work would be a good exercise. Not many programming languages allow you to use block closures like this.

 

Paul Baumann

 

 

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Donald MacQueen
Sent: Friday, April 11, 2014 14:09
To: Roelof Wobben; [hidden email]
Subject: Re: [vwnc] Fwd: Re: random number between 2 numbers

 

On 4/11/2014 1:48 PM, Roelof Wobben wrote:


Roelof,

You can use anything you want as a dictionary key.  You could try representing your grid as an Array of Arrays. 

| grid |

grid := Array new.
grid add: (Array with: 1 with: 2).
grid add: (Array with: 3 with: 4).

To retrieve the value 2, inspect (grid first) last.

If you are coming from another language, an excellent start is the book Smalltalk by Example available here: http://stephane.ducasse.free.fr/FreeBooks.html

I am not a VW guy, but they may have a Matrix class.

Keep trying.




Thanks,

I have read the document and I doubt between a Directonary or a Array.
I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

Roelof

 





_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc




-- 
Donald [|]
 
Joel Cairo: "You always have a very smooth explanation."
Sam Spade: "What do you want me to do? Learn to stutter?"
- The Maltese Falcon


This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Paul Baumann schreef op 11-4-2014 23:57:

Hi Roelof,

 

A little more detail...

 

>> I have read the document and I doubt between a Directonary or a Array.
>> I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

 

Array can only be accessed with integer "keys", and only within the size allocated. Arrays don't grow in VisualWorks and most other Smalltalk dialects, so if you need growth then use an OrderedCollection instead of an Array. Both OC and Array will error if you attempt to access a position greater than the size allocated or less than 1. Arrays and OC are extremely fast at finding an object for an integer key but you wouldn't want to use them for a sparsely populated collection because a slot is allocated for every possible key. Use a Dictionary unless you are certain that an Array meets your needs.

 

A Dictionary allows "any" kind of object (except nil) to be used as a key. An object that is used as the key of a dictionary is first sent #hash to determine where to start looking for an equal key, keys are then compared with #= equality tests to find a match. If you use your own objects as keys then you sometimes want to define what it means for two of those objects to be considered equal. Equal objects need to return true from #=, but what most people new to Smalltalk miss is that those equal objects need to also answer an equal number from #hash so that hashed collections (like Dictionary and Set for example) start looking for a match at the correct index position. Kernel objects like integer and point (the examples you gave) already define both #= and #hash. Most people use common objects like strings and integers for dictionary keys instead of defining what it means for two of their domain objects to be considered equal.

 

An interval is an object that is similar to the "(1 - 16)...or 1@1" object you seemed to be considering as a key. I suspect you are thinking of using a dictionary different from how it is designed to operate. This demonstrates that key like that is one object and not many:

 

Dictionary new

            at: (1 to: 16) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#findme

 

Dictionary new

            at: (1 to: 1000) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#nothing

 

Here are examples of filling with a range of keys:

 

| index |

index := OrderedCollection new.

1 to: 16 do: [:i | index at: i put: #findme].

index at: 15

 

or:

 

| index |

index := Dictionary new.

1 to: 16 do: [:i | index at: i put: #findme ].

index at: 15

 

Your original question seemed to be more about how to get random numbers within a range. This is an example that uses a block to get 1000 random integers between 33 and 64:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [(randomStream next * range size + range first) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

Or, now reading randomly from values in the range interval:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

And now an example of randomly selecting from a collection of specific values:

 

| range randomStream aBlock |

randomStream := Random new.

range := #(43 61 89 127 181 257 367 521 739 1049 1481 2099).

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

All your "power of 2" numbers can be in the 'range' collection above. You could randomly select any object from a collection in the same way. Figuring out how the examples work would be a good exercise. Not many programming languages allow you to use block closures like this.

 

Paul Baumann

 



Correct, that was my original question.
What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function  a "2" and all the others empty.

I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.

I will study your examples so I can learn more about Collectiions and how they work.

Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Roelof Wobben schreef op 12-4-2014 8:40:
Paul Baumann schreef op 11-4-2014 23:57:

Hi Roelof,

 

A little more detail...

 

>> I have read the document and I doubt between a Directonary or a Array.
>> I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

 

Array can only be accessed with integer "keys", and only within the size allocated. Arrays don't grow in VisualWorks and most other Smalltalk dialects, so if you need growth then use an OrderedCollection instead of an Array. Both OC and Array will error if you attempt to access a position greater than the size allocated or less than 1. Arrays and OC are extremely fast at finding an object for an integer key but you wouldn't want to use them for a sparsely populated collection because a slot is allocated for every possible key. Use a Dictionary unless you are certain that an Array meets your needs.

 

A Dictionary allows "any" kind of object (except nil) to be used as a key. An object that is used as the key of a dictionary is first sent #hash to determine where to start looking for an equal key, keys are then compared with #= equality tests to find a match. If you use your own objects as keys then you sometimes want to define what it means for two of those objects to be considered equal. Equal objects need to return true from #=, but what most people new to Smalltalk miss is that those equal objects need to also answer an equal number from #hash so that hashed collections (like Dictionary and Set for example) start looking for a match at the correct index position. Kernel objects like integer and point (the examples you gave) already define both #= and #hash. Most people use common objects like strings and integers for dictionary keys instead of defining what it means for two of their domain objects to be considered equal.

 

An interval is an object that is similar to the "(1 - 16)...or 1@1" object you seemed to be considering as a key. I suspect you are thinking of using a dictionary different from how it is designed to operate. This demonstrates that key like that is one object and not many:

 

Dictionary new

            at: (1 to: 16) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#findme

 

Dictionary new

            at: (1 to: 1000) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#nothing

 

Here are examples of filling with a range of keys:

 

| index |

index := OrderedCollection new.

1 to: 16 do: [:i | index at: i put: #findme].

index at: 15

 

or:

 

| index |

index := Dictionary new.

1 to: 16 do: [:i | index at: i put: #findme ].

index at: 15

 

Your original question seemed to be more about how to get random numbers within a range. This is an example that uses a block to get 1000 random integers between 33 and 64:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [(randomStream next * range size + range first) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

Or, now reading randomly from values in the range interval:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

And now an example of randomly selecting from a collection of specific values:

 

| range randomStream aBlock |

randomStream := Random new.

range := #(43 61 89 127 181 257 367 521 739 1049 1481 2099).

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

All your "power of 2" numbers can be in the 'range' collection above. You could randomly select any object from a collection in the same way. Figuring out how the examples work would be a good exercise. Not many programming languages allow you to use block closures like this.

 

Paul Baumann

 



Correct, that was my original question.
What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function  a "2" and all the others empty.

I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.

I will study your examples so I can learn more about Collectiions and how they work.

Roelof


I see one little problem. With this script it is possible that the same number gets "choosen" two times.
So I have to use a set I think to solve that one.

Roelof



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Roelof Wobben schreef op 12-4-2014 10:04:
Roelof Wobben schreef op 12-4-2014 8:40:
Paul Baumann schreef op 11-4-2014 23:57:

Hi Roelof,

 

A little more detail...

 

>> I have read the document and I doubt between a Directonary or a Array.
>> I will test if the number ( 1- 16) can be a key for a Directonary or that I still can use something like "1@1" as key.

 

Array can only be accessed with integer "keys", and only within the size allocated. Arrays don't grow in VisualWorks and most other Smalltalk dialects, so if you need growth then use an OrderedCollection instead of an Array. Both OC and Array will error if you attempt to access a position greater than the size allocated or less than 1. Arrays and OC are extremely fast at finding an object for an integer key but you wouldn't want to use them for a sparsely populated collection because a slot is allocated for every possible key. Use a Dictionary unless you are certain that an Array meets your needs.

 

A Dictionary allows "any" kind of object (except nil) to be used as a key. An object that is used as the key of a dictionary is first sent #hash to determine where to start looking for an equal key, keys are then compared with #= equality tests to find a match. If you use your own objects as keys then you sometimes want to define what it means for two of those objects to be considered equal. Equal objects need to return true from #=, but what most people new to Smalltalk miss is that those equal objects need to also answer an equal number from #hash so that hashed collections (like Dictionary and Set for example) start looking for a match at the correct index position. Kernel objects like integer and point (the examples you gave) already define both #= and #hash. Most people use common objects like strings and integers for dictionary keys instead of defining what it means for two of their domain objects to be considered equal.

 

An interval is an object that is similar to the "(1 - 16)...or 1@1" object you seemed to be considering as a key. I suspect you are thinking of using a dictionary different from how it is designed to operate. This demonstrates that key like that is one object and not many:

 

Dictionary new

            at: (1 to: 16) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#findme

 

Dictionary new

            at: (1 to: 1000) put: #findme;

            at: (1 to: 16) ifAbsent: [#nothing]

#nothing

 

Here are examples of filling with a range of keys:

 

| index |

index := OrderedCollection new.

1 to: 16 do: [:i | index at: i put: #findme].

index at: 15

 

or:

 

| index |

index := Dictionary new.

1 to: 16 do: [:i | index at: i put: #findme ].

index at: 15

 

Your original question seemed to be more about how to get random numbers within a range. This is an example that uses a block to get 1000 random integers between 33 and 64:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [(randomStream next * range size + range first) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

Or, now reading randomly from values in the range interval:

 

| range randomStream aBlock |

randomStream := Random new.

range := 33 to: 64.

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

And now an example of randomly selecting from a collection of specific values:

 

| range randomStream aBlock |

randomStream := Random new.

range := #(43 61 89 127 181 257 367 521 739 1049 1481 2099).

aBlock := [range at: 1 + (randomStream next * range size) asInteger].

(1 to: 1000) collect: [:i | aBlock value ].

 

All your "power of 2" numbers can be in the 'range' collection above. You could randomly select any object from a collection in the same way. Figuring out how the examples work would be a good exercise. Not many programming languages allow you to use block closures like this.

 

Paul Baumann

 



Correct, that was my original question.
What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function  a "2" and all the others empty.

I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.

I will study your examples so I can learn more about Collectiions and how they work.

Roelof


I see one little problem. With this script it is possible that the same number gets "choosen" two times.
So I have to use a set I think to solve that one.

Roelof

yep, If I make ablock a set everything works as expected.



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Paul Baumann
In reply to this post by Roelof

 

>>From: Roelof Wobben

>>What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function

>>  a "2" and all the others empty.

 

Not sure what you mean by (a "2" and all the others empty), but this is one way to do the first part using a 2D literal array:


| fourByFour randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            pos -> ((fourByFour at: pos y) at: pos x).

].

 

>>I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.

This shows converting the 4x4 grid to a dictionary with points as keys, and then randomly gets 100 of them:

 

| fourByFour dict randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

dict := Dictionary new.

fourByFour doWithIndex: [:yArray :y |

            yArray doWithIndex: [:value :x |

                        dict at: (x @ y) put: value

            ].

].

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            dict associationAt: pos.

].

 

More efficient access would be from flattening the 2D grid to a 1D sequenceable collection and using a single random (no point creation) for access:

 

| fourByFour all randomStream |

randomStream := Random new.

fourByFour := #(

                (1 2 3 4)

                (5 6 7 8)

                (9 10 11 12)

                (13 14 15 16)

).

all := OrderedCollection new.

fourByFour doWithIndex: [:yArray :y |

                yArray doWithIndex: [:value :x |

                                all add: (x @ y) -> value

                ].

].

(1 to: 100) collect: [:i |

                | pos |

                pos := (randomStream next * all size) asInteger + 1.

                all at: pos.

].

 

Paul Baumann

 

 



This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Roelof
Paul Baumann schreef op 14-4-2014 16:55:

 

>>From: Roelof Wobben

>>What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function

>>  a "2" and all the others empty.

 

Not sure what you mean by (a "2" and all the others empty), but this is one way to do the first part using a 2D literal array:


| fourByFour randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            pos -> ((fourByFour at: pos y) at: pos x).

].

 

>>I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.

This shows converting the 4x4 grid to a dictionary with points as keys, and then randomly gets 100 of them:

 

| fourByFour dict randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

dict := Dictionary new.

fourByFour doWithIndex: [:yArray :y |

            yArray doWithIndex: [:value :x |

                        dict at: (x @ y) put: value

            ].

].

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            dict associationAt: pos.

].

 

More efficient access would be from flattening the 2D grid to a 1D sequenceable collection and using a single random (no point creation) for access:

 

| fourByFour all randomStream |

randomStream := Random new.

fourByFour := #(

                (1 2 3 4)

                (5 6 7 8)

                (9 10 11 12)

                (13 14 15 16)

).

all := OrderedCollection new.

fourByFour doWithIndex: [:yArray :y |

                yArray doWithIndex: [:value :x |

                                all add: (x @ y) -> value

                ].

].

(1 to: 100) collect: [:i |

                | pos |

                pos := (randomStream next * all size) asInteger + 1.

                all at: pos.

].

 

Paul Baumann

 

 



This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

Thanks,

What Im looking for is it.
I need a 4 X 4 grid where 2 tiles have a value of 2.
The rest can be empty or 0
The two tiles are random choosen out of all the tiles.

Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Re: random number between 2 numbers

Paul Baumann

 

>> From: [hidden email] [mailto:[hidden email]] On Behalf Of Roelof Wobben

>> What Im looking for is it.
>> I need a 4 X 4 grid where 2 tiles have a value of 2.
>> The rest can be empty or 0
>> The two tiles are random choosen out of all the tiles.

| randomStream all count as dest  |

randomStream := Random new.

all := ByteArray new: 16.

count := 0.

[

            | pos |

            pos := (randomStream next * all size) asInteger + 1.

            (all at: pos) = 0 ifTrue: [count := count + 1].

            all at: pos put: 2.

            count < 2

] whileTrue: [].

as := (Array new: 4) writeStream.

dest := nil.

all doWithIndex: [:ea :i |

            i \\ 4 = 1 ifTrue: [as nextPut: (dest := (Array new: 4) writeStream)].

            dest nextPut: ea.

].

as contents collect: [:ws | ws contents ]

 

#(#(0 0 0 2) #(0 0 0 2) #(0 0 0 0) #(0 0 0 0))

#(#(2 0 0 0) #(0 0 0 0) #(0 0 2 0) #(0 0 0 0))

#(#(0 2 0 0) #(0 0 0 0) #(0 0 2 0) #(0 0 0 0))

#(#(0 0 2 2) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0))

#(#(0 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 2 0 2))

 



This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

FW: Fwd: Re: random number between 2 numbers

Thomas, Arden
In reply to this post by Roelof
<script type="text/javascript" id="cosymantecbfw_resize">var kvcPushDownExceptions = {}; var retryCSSHack = 0; var isUrlExcepted = function() { if(!window.location || !kvcPushDownExceptions) { return false; } var path = kvcPushDownExceptions[window.location.host]; if(!path) { return false; } var patt = new RegExp(path); return patt.test(window.location.pathname); }; document.addEventListener("initializeExceptionListEvent", function(e) { if(!e || !e.detail) { return; } kvcPushDownExceptions = e.detail; }, false); var coAdjustToolbarHeight = function(h) { var e = document.getElementById("cosymantecbfw"); if(e) { var nOldHeight = 0; if(window.getComputedStyle(e,null)) { var height = window.getComputedStyle(e,null).getPropertyValue("height"); var oldHeight = height.replace("px", ""); nOldHeight = Number(oldHeight); if(!isNaN(nOldHeight)) { nOldHeight = 0; } } if(document.readyState === "complete") { applyCSSHack(nOldHeight, h); } else { window.addEventListener("onload", applyCSSHack(nOldHeight, h), false); } e.style.height = h + "px"; e.style.left = window.pageXOffset + "px"; } }; var arrayElements = new Array(); var applyCSSHack = function( oldHeight, newHeight) { if(isUrlExcepted()) { return; } if(!document.body) { if(retryCSSHack++ < 30) { setTimeout(function() { applyCSSHack(oldHeight, newHeight); }, 100); } return; } arrayElements = new Array(); if(document.body.style) { document.body.style.position = "relative"; var len = newHeight + "px"; document.body.style.setProperty("margin-top", len, "important"); document.body.style.setProperty("border", "1px solid transparent", "important"); } walk(document.body); for(var i = 0; i < arrayElements.length; i++) { var elem = arrayElements[i]; if(null == window.getComputedStyle(elem,null)) { continue; } var pos = window.getComputedStyle(elem,null).getPropertyValue("position"); if(pos !== "fixed") { continue; } if(elem.parentNode == document.body) { elem.style.position = "absolute"; continue; } var oldTop = window.getComputedStyle(elem,null).getPropertyValue("top"); oldTop = oldTop.replace("px", ""); var nOldTop = Number(oldTop); if(!isNaN(nOldTop)) { nOldTop = 0; } var top = nOldTop + (Number(newHeight) - Number(oldHeight)); if(elem.style) { elem.style.top = top + "px"; } } }; var walk = function (elm) { if(window.getComputedStyle(elm,null) && "auto" !== window.getComputedStyle(elm,null).getPropertyValue("top")) { arrayElements.push(elm); } var node; for (node = elm.firstChild; node; node = node.nextSibling) { if (node.nodeType === 1) { walk(node); } } };</script><script type="text/javascript" id="waxCS">var WAX = function () { var _arrInputs; return { getElement: function (i) { return _arrInputs[i]; }, setElement: function(i){ _arrInputs=i; } } }(); function waxGetElement(i) { return WAX.getElement(i); } function coSetPageData(t, d){ if('wax'==t) { WAX.setElement(d);} }</script><script type="text/javascript" id="waxCS">var WAX = function () { var _arrInputs; return { getElement: function (i) { return _arrInputs[i]; }, setElement: function(i){ _arrInputs=i; } } }(); function waxGetElement(i) { return WAX.getElement(i); } function coSetPageData(t, d){ if('wax'==t) { WAX.setElement(d);} }</script>
Hi  Roelof, et al;

It sounds like you may be doing exactly what I just did - implementing "2048".
If not you still may find it of interest.  
It displays a 4x4 grid, and uses Random for determining which empty space to add a new value.

I published a basic version of this on the Cincom Public Repository.

Check out:

hth


Arden Thomas
Cincom Smalltalk Product Manager


From: [hidden email] [mailto:[hidden email]] On Behalf Of Roelof Wobben
Sent: Monday, April 14, 2014 11:12 AM
To: [hidden email]
Subject: Re: [vwnc] Fwd: Re: random number between 2 numbers

 

Paul Baumann schreef op 14-4-2014 16:55:

 

>>From: Roelof Wobben

>>What I try to figure out is the next step. How can I represent the 4 x 4 grid with on the two positions choosen by the random function

>>  a "2" and all the others empty.

 

Not sure what you mean by (a "2" and all the others empty), but this is one way to do the first part using a 2D literal array:


| fourByFour randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            pos -> ((fourByFour at: pos y) at: pos x).

].

 

>>I thought I could use the position of the grid but maybe I can better use the numbers 1 - 16.


This shows converting the 4x4 grid to a dictionary with points as keys, and then randomly gets 100 of them:

 

| fourByFour dict randomStream |

fourByFour := #(

            (1 2 3 4)

            (5 6 7 8)

            (9 10 11 12)

            (13 14 15 16)

).

dict := Dictionary new.

fourByFour doWithIndex: [:yArray :y |

            yArray doWithIndex: [:value :x |

                        dict at: (x @ y) put: value

            ].

].

randomStream := Random new.

(1 to: 100) collect: [:i |

            | pos |

            pos := ((randomStream next * 4) asInteger + 1) @ ((randomStream next * 4) asInteger + 1).

            dict associationAt: pos.

].

 

More efficient access would be from flattening the 2D grid to a 1D sequenceable collection and using a single random (no point creation) for access:

 

| fourByFour all randomStream |

randomStream := Random new.

fourByFour := #(

                (1 2 3 4)

                (5 6 7 8)

                (9 10 11 12)

                (13 14 15 16)

).

all := OrderedCollection new.

fourByFour doWithIndex: [:yArray :y |

                yArray doWithIndex: [:value :x |

                                all add: (x @ y) -> value

                ].

].

(1 to: 100) collect: [:i |

                | pos |

                pos := (randomStream next * all size) asInteger + 1.

                all at: pos.

].

 

Paul Baumann

 

 

 


This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.


Thanks,

What Im looking for is it.
I need a 4 X 4 grid where 2 tiles have a value of 2.
The rest can be empty or 0
The two tiles are random choosen out of all the tiles.

Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

Untitled attachment 03827.txt (186 bytes) Download Attachment