# random number between 2 numbers

7 messages
Open this post in threaded view
|

## random number between 2 numbers

 Hello, I m looking at the game 2048. I think I will try to make this one but then in smalltalk. At the beginning you have a 4 x 4 grid where 2 of the boxes have the number 2. Now I wonder how can I take care the the 2 boxes which contains the number 2 are random choosen. I know Visualworks has a Random Class but I do not see how I can make it work so a number between 1 and 16 is choosen two times. Also later on the game I have to make it work that random numbers are added but they have to be a number which is a power of 2. How can I solve both cases ? Roelof _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Open this post in threaded view
|

## Re: random number between 2 numbers

 Hi Roelof! On 10.04.2014 08:09, Roelof Wobben wrote: > I know Visualworks has a Random Class but I do not see how I can make it > work so a number between 1 and 16 is choosen two times. As with most programming languages, a random number generator usually returns a value in a specific range. Depending on the concrete subclass of Random, the range of the returned value varies. Most of them return floating point values in the range [0..1). So if you want to get numbers from the range [1..16] you need to scale and shift it like   number := ((random * 16) + 1) truncated. > Also later on the game I have to make it work that random numbers are > added but they have to be a number which is a power of 2. If all numbers need to be powers of two, you can use a random number generator to create the exponent, just scale the exponent to your needs. And a side note: Since the implementations are not truly random, your should use them as expected: Reuse a initialized Random object, do not create a new object for every #next call. Regards   Jan _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Open this post in threaded view
|

## Re: random number between 2 numbers

 Jan Weerts schreef op 10-4-2014 11:24: > Hi Roelof! > > On 10.04.2014 08:09, Roelof Wobben wrote: >> I know Visualworks has a Random Class but I do not see how I can make it >> work so a number between 1 and 16 is choosen two times. > As with most programming languages, a random number generator usually returns a value in a specific range. Depending on the concrete subclass of Random, the range of the returned value varies. Most of them return floating point values in the range [0..1). So if you want to get numbers from the range [1..16] you need to scale and shift it like >    number := ((random * 16) + 1) truncated. > >> Also later on the game I have to make it work that random numbers are >> added but they have to be a number which is a power of 2. > If all numbers need to be powers of two, you can use a random number generator to create the exponent, just scale the exponent to your needs. > > And a side note: Since the implementations are not truly random, your should use them as expected: Reuse a initialized Random object, do not create a new object for every #next call. > Hmm, that can be a problem.  As long as the games runs a new object is added with a number which must be a power of 2. You can see the game running here : http://gabrielecirulli.github.io/2048/Roelof _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Open this post in threaded view
|

## Re: random number between 2 numbers

 In reply to this post by Roelof Peter Goessaert schreef op 10-4-2014 10:47: ```| rnd | Transcript clear. rnd := Random new. 2048 timesRepeat: [ `|randomNumber|` randomNumber := (rnd next * 15) rounded + 1. " 1-> 16 included " Transcript tab; show: randomNumber]. ``` Sorry but i do not work. When I copie this in a workspace I see a Message not understood ; astring. Roelof _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Open this post in threaded view
|

## Re: random number between 2 numbers

 Jan Weerts schreef op 11-4-2014 10:24: > Hi Roelof! > > The problem is, that randomNumber is not a string but a number and #show: expects a string. > You should be able to see the problem in the debugger. This is a very, very, very (did i mention "very") powerful tool in the smalltalk ide. Some people even develop only using the debugger and i myself find me often tweaking a method multiple times in the debugger until it works as expected. > > Thanks, You are right except this time I did not fully understand the error message. Roelof _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc