Re: Random Number code

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

Re: Random Number code

Roelof
Michael Gross schreef op 10-4-2014 20:40:

| rnd mx|

Transcript clear.

rnd := Random new.

2048 timesRepeat: [ |randomNumber|

               randomNumber := (rnd next * 15) rounded + 1. " 1-> 16 included "

      mx := randomNumber printString.

               Transcript tab; show:  mx].


Hello,

It worked but I see something like this :

4 4  and this cannot be happen.

and the first 5 attempts gives this result after I changed 2048 to 2 because I need 2 numbers.

3  8 

15 10

12 9

15 7

4  4

Roelof


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

Re: Random Number code

Joerg Beekmann, DeepCove Labs (YVR)

Aside from generating a sequence of random numbers what is it you are after?

 

Refactoring what you have and displaying the results as counts gives the below. That is the start of something generating a uniformly distributed sequence of integers in the range 1 to 16. Note that issue with 1 and 16 which are under represented by about 50%.

 

 

 

| rnd nums |

Transcript clear.

rnd := Random new.

nums := (1 to: 100000) collect: [:i| (rnd next * 15) rounded + 1].

nums asBag sortedCounts do: #out

3350 -> 16

3419 -> 1

6439 -> 6

6574 -> 2

6620 -> 10

6635 -> 14

6658 -> 9

6663 -> 7

6683 -> 15

6687 -> 11

6693 -> 13

6706 -> 12

6707 -> 8

6708 -> 3

6721 -> 5

6737 -> 4

From: [hidden email] [mailto:[hidden email]] On Behalf Of Roelof Wobben
Sent: Thursday, April 10, 2014 11:58 AM
To: [hidden email]
Subject: Re: [vwnc] Random Number code

 

Michael Gross schreef op 10-4-2014 20:40:

| rnd mx|

Transcript clear.

rnd := Random new.

2048 timesRepeat: [ |randomNumber|

               randomNumber := (rnd next * 15) rounded + 1. " 1-> 16 included "

      mx := randomNumber printString.

               Transcript tab; show:  mx].


Hello,

It worked but I see something like this :

4 4  and this cannot be happen.

and the first 5 attempts gives this result after I changed 2048 to 2 because I need 2 numbers.

3  8 

15 10

12 9

15 7

4  4

Roelof


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

Re: Random Number code

Phil Helms
In reply to this post by Roelof
Does the multiplier need to be 16 instead of 15?

--
Phil

On 04/10/2014 12:57 PM, Roelof Wobben wrote:
Michael Gross schreef op 10-4-2014 20:40:

| rnd mx|

Transcript clear.

rnd := Random new.

2048 timesRepeat: [ |randomNumber|

               randomNumber := (rnd next * 15) rounded + 1. " 1-> 16 included "

      mx := randomNumber printString.

               Transcript tab; show:  mx].


Hello,

It worked but I see something like this :

4 4  and this cannot be happen.

and the first 5 attempts gives this result after I changed 2048 to 2 because I need 2 numbers.

3  8 

15 10

12 9

15 7

4  4

Roelof



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


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

Re: Random Number code

Roelof
In reply to this post by Joerg Beekmann, DeepCove Labs (YVR)
Joerg Beekmann, DeepCove Labs schreef op 10-4-2014 21:19:

Aside from generating a sequence of random numbers what is it you are after?

 

Refactoring what you have and displaying the results as counts gives the below. That is the start of something generating a uniformly distributed sequence of integers in the range 1 to 16. Note that issue with 1 and 16 which are under represented by about 50%.

 

 

 

| rnd nums |

Transcript clear.

rnd := Random new.

nums := (1 to: 100000) collect: [:i| (rnd next * 15) rounded + 1].

nums asBag sortedCounts do: #out

3350 -> 16

3419 -> 1

6439 -> 6

6574 -> 2

6620 -> 10

6635 -> 14

6658 -> 9

6663 -> 7

6683 -> 15

6687 -> 11

6693 -> 13

6706 -> 12

6707 -> 8

6708 -> 3

6721 -> 5

6737 -> 4



what I try to achieve is to make this game ( http://gabrielecirulli.github.io/2048/) in Smaltalk.
So I need 2 places where something is shown and I want to achieve that is every time another game.

Roelof


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

Re: Random Number code

Peter Goessaert
In reply to this post by Roelof

Ok, I misunderstood.

 

This does what you need I think:

 

| rnd numbers|

numbers := Set new.

rnd := Random new.

[numbers size < 2] whileTrue: [ |randomNumber|

               randomNumber := (rnd next * 15) truncated + 1.

                   numbers add: randomNumber].

numbers inspect

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Roelof Wobben
Sent: donderdag 10 april 2014 20:58
To: [hidden email]
Subject: Re: [vwnc] Random Number code

 

Michael Gross schreef op 10-4-2014 20:40:

| rnd mx|

Transcript clear.

rnd := Random new.

2048 timesRepeat: [ |randomNumber|

               randomNumber := (rnd next * 15) rounded + 1. " 1-> 16 included "

      mx := randomNumber printString.

               Transcript tab; show:  mx].


Hello,

It worked but I see something like this :

4 4  and this cannot be happen.

and the first 5 attempts gives this result after I changed 2048 to 2 because I need 2 numbers.

3  8 

15 10

12 9

15 7

4  4

Roelof


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

Re: Random Number code

Roelof
Peter Goessaert schreef op 10-4-2014 21:46:

| rnd numbers|

numbers := Set new.

rnd := Random new.

[numbers size < 2] whileTrue: [ |randomNumber|

               randomNumber := (rnd next * 15) truncated + 1.

                   numbers add: randomNumber].

numbers inspect


Thanks,

Does the  *15 take care of that only 1 till 16 is chosen.

Im corius how this script is working.

Roelof


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

Re: Random Number code

Roelof
In reply to this post by Peter Goessaert
Peter Goessaert schreef op 10-4-2014 21:46:

Ok, I misunderstood.

 

This does what you need I think:

 

| rnd numbers|

numbers := Set new.

rnd := Random new.

[numbers size < 2] whileTrue: [ |randomNumber|

               randomNumber := (rnd next * 15) truncated + 1.

                   numbers add: randomNumber].

numbers inspect

 



Thanks,

I changed it a little bit so the numbers get printed one by one.

| rnd numbers cijfer |
numbers := Set new.
rnd := Random new.
[numbers size < 2] whileTrue: [ |randomNumber|
               randomNumber := (rnd next * 15) truncated + 1.
                numbers add: randomNumber].
numbers do: [:cijfer | Transcript show cijfer; cr]

But now I see a unbound message do: and a message that cijfer is not used.

Roelof





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

Re: Random Number code

Nowak, Helge

| rnd numbers  |

numbers := Set new.

rnd := Random new.

[numbers size < 2] whileTrue: [ |randomNumber|

               randomNumber := (rnd next * 15) truncated + 1.

                numbers add: randomNumber].

numbers do: [:cijfer | Transcript show: cijfer printString; cr]

 

Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roelof Wobben
Gesendet: Freitag, 11. April 2014 08:57
An: [hidden email]
Betreff: Re: [vwnc] Random Number code

 

Peter Goessaert schreef op 10-4-2014 21:46:

Ok, I misunderstood.

 

This does what you need I think:

 

| rnd numbers|

numbers := Set new.

rnd := Random new.

[numbers size < 2] whileTrue: [ |randomNumber|

               randomNumber := (rnd next * 15) truncated + 1.

                   numbers add: randomNumber].

numbers inspect

 

 


Thanks,

I changed it a little bit so the numbers get printed one by one.

| rnd numbers cijfer |
numbers := Set new.
rnd := Random new.
[numbers size < 2] whileTrue: [ |randomNumber|
               randomNumber := (rnd next * 15) truncated + 1.
                numbers add: randomNumber].
numbers do: [:cijfer | Transcript show cijfer; cr]

But now I see a unbound message do: and a message that cijfer is not used.

Roelof




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

Re: Random Number code

Steffen Märcker
In reply to this post by Roelof
Hi,

you have a typo in your code. The message show: is missing the colon. Additionally, you don't have to declare cijfer, since you only use it in the scope of the block.

| rnd numbers |
numbers := Set new.
rnd := Random new.
[numbers size < 16] whileTrue:
[| randomNumber |
randomNumber := (rnd next * 15) truncated + 1.
numbers add: randomNumber].
numbers do:
[:cijfer |
Transcript
show: cijfer printString;
cr]

Regards, Steffen

Am 11.04.2014, 08:57 Uhr, schrieb Roelof Wobben <[hidden email]>:

| rnd numbers cijfer | numbers := Set new. rnd := Random new. [numbers size < 2] whileTrue: [ |randomNumber| randomNumber := (rnd next * 15) truncated + 1. numbers add: randomNumber]. numbers do: [:cijfer | Transcript show cijfer; cr]




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