# 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
Open this post in threaded view
|

## Re: random number between 2 numbers

 Roelof Wobben schreef op 11-4-2014 13:27: > 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. >> >> > Another question: Is there info how I can take care that I make a new set based on the contents of numbers. So lets say that numbers contains 2 and 5. I want to make a new set which contains a 2 at position 2 and position 5. I tried this numbers do: [cijfer | grid at: cijfer put: 2 ] but that is not working. 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

 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, > > Another question: > > Is there info how I can take care that I make a new set based on the > contents of numbers. > So lets say that numbers contains 2 and 5. I want to make a new set > which contains a 2 at position 2 and position 5. > I tried this numbers do: [cijfer | grid at: cijfer put: 2 ] but that > is not working. > > 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