# OR in a random block

**URL:** https://forum.makecode.com/t/or-in-a-random-block/23798
**Category:** Arcade
**Created:** [October 30, 2023, 9:46pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798 "2023-10-30T21:46:58Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [October 30, 2023, 9:46pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/1 "2023-10-30T21:46:58Z")

</div>

I want to generate a random variable that is in 1 of 2 ranges. Either 30 to 60 OR -30 to -60. So, the number could be a value from one of either range, but not between -30 and +30. So far, I have a very long process of using %chance to choose one of the 2 ranges, then the random block to choose within that range. Is there a cleaner way to do this?

> **[test](https://arcade.makecode.com/56699-74285-32392-02043)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)
#### Post date: [October 30, 2023, 10:07pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/2 "2023-10-30T22:07:35Z")

</div>

Is this what you want?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/64aada942e1bc49b059d8b9a19e43508c92cc279.png)

---

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [October 30, 2023, 10:26pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/3 "2023-10-30T22:26:35Z")

</div>

Yes! That’s much more elegant! Thanks so much!

---

<div class="post-metadata">

### Author: ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)
#### Post date: [October 30, 2023, 11:05pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/4 "2023-10-30T23:05:29Z")

</div>

Never as fast or elegant as @Blobbey, but an interesting challenge and here is my take:

> **[test](https://arcade.makecode.com/07921-14850-57660-23079)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [October 31, 2023, 5:33pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/5 "2023-10-31T17:33:30Z")

</div>

I like this one too, but it’s a bit too complex for my students. I always like to see how other people see solutions to a challenge.

---

<div class="post-metadata">

### Author: ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)
#### Post date: [October 31, 2023, 8:38pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/6 "2023-10-31T20:38:55Z")

</div>

Hmm, that function call was really expensive.

Here’s a last take which is at least relatively fast, but just as many LOC cluttering _On start()_ as your original solution.

> **[test](https://arcade.makecode.com/19718-49479-05192-60558)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

(Please remove the loop and _millis_, this was just me playing around to benchmark the different solutions’ relative speeds)

I guess @Blobbey’s solution stands as the neatest and best used for teaching students. I am really surprised that it also performs relatively well compared to a function(even an almost empty function), with its 2-dimensional array created and populated for every iteration, 3 random operations and one traversal of and lookup in array…). What is a day without learning something new? 😉

I would like to see somebody come up with an even more optimized solution; short, beautiful AND blazing fast! 😉

---

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [October 31, 2023, 11:03pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/7 "2023-10-31T23:03:46Z")

</div>

I have to admit, Vegz78, your solution was a bit over my head. I’d have to really explore it to understand it. What is the speed that you are timing? The speed that the variable is selected or the speed until it populates the sprite in level 2 with a random velocity? I am not sure I’d even notice that.

---

<div class="post-metadata">

### Author: ![ChimbroDaPro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/chimbrodapro/32/15852_2.png) [@ChimbroDaPro](https://forum.makecode.com/u/ChimbroDaPro)
#### Post date: [November 1, 2023, 6:11pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/8 "2023-11-01T18:11:30Z")

</div>

Looks smart. 🙂

---

<div class="post-metadata">

### Author: ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)
#### Post date: [November 1, 2023, 6:53pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/9 "2023-11-01T18:53:08Z")

</div>

> [@bee](#):
>
> I am not sure I’d even notice that.

Probably not, unless you are in need of A LOT of such random draws within those special ranges…

> [@bee](#):
>
> I have to admit, Vegz78, your solution was a bit over my head. I’d have to really explore it to understand it.

Yeah, it was initially an attempt to get the ranges you were looking for with only 1 throw of the dice pr. number in such special ranges which are on both sides of 0, but not continuous across 0, which quickly grew very complex when trying to figure it out. Not a very neat solution. But since the distribution should be uniform, it probably doesn’t matter with multiple random draws pr. number produced.

> [@bee](#):
>
> What is the speed that you are timing? The speed that the variable is selected or the speed until it populates the sprite in level 2 with a random velocity?

I am timing(or attempting to time) from just before to just after a large loop which which draws a huge number of random numbers inside those special ranges and storing it to the _Random1_ variable. Given that there are now 4 solutions presented, including your initial one, I got curious about their relative speeds.

Then I was surprised about how much time my first solution used, and closer investigation pointed me towards that it was the function call itself that spent most of the time, not the overly complex algorithm. I knew that function calls have a cost, but I did not know it was such a huge relative cost(but insignificant and more than fast enough for most use cases and programs, as you point out, except when there is a very high number of iterations…)

Just for the special geeky interest of it, I posted these results, because maybe someone a) had a better algorithm or b) more information/experience to share about the relatively slow function calls in MakeCode?

I should have posted only my last solution without all the measurement code, and the function part in its own thread. Sorry for hijacking/steering your thread a little away from its initial direction. 😉

---

<div class="post-metadata">

### Author: ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)
#### Post date: [November 1, 2023, 7:44pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/10 "2023-11-01T19:44:43Z")

</div>

I do a variation of your original algorithm: Pick a random value and, half the time, change it to a negative value.

 ![arcade-screenshot (8)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/c/c5cfd51f8cd16ad3def446f9e84528d9b331e097.png)

@Blobbey 's version, though, seems easier to understand.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [November 2, 2023, 4:07pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/11 "2023-11-02T16:07:59Z")

</div>

Wow, now that’s clean. Gotta be my favorite implementation so far!

---

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [November 2, 2023, 4:09pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/12 "2023-11-02T16:09:34Z")

</div>

> [@Vegz78](#):
>
> I should have posted only my last solution without all the measurement code, and the function part in its own thread. Sorry for hijacking/steering your thread a little away from its initial direction. 😉

No, I appreciate the details, it helps me (and likely others) learn! I love wandering into a thread that has someone’s though processes explained. It may not be useful for THIS problem, but someone, somewhere is going to need this bit of code.

---

<div class="post-metadata">

### Author: ![bee](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bee](https://forum.makecode.com/u/bee)
#### Post date: [November 2, 2023, 4:09pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/13 "2023-11-02T16:09:41Z")

</div>

> [@AlexK](#):
>
> I do a variation of your original algorithm: Pick a random value and, half the time, change it to a negative value.

Oh, this is an elegant solution! It would be really great for my students! Array’s are a bit harder to explain, whereas this is basic math that would be easier to understand for 10 yr olds! Thanks for sharing.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [November 2, 2023, 5:50pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/14 "2023-11-02T17:50:51Z")

</div>

Okay, I can’t resist any longer, I must chime in with my take on the problem

 ![arcade-screenshot (2)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/015ca341a433a8fa149419087df802e4e5e019f6.png)

Instead of doing two separate random generations, I instead set a negative multiplier half the time, generate a number from 30-60 and then multiply it with the multiplier. This way, half of the time the result is negative, achieving the desired effect.

BTW, I like how this thread has become a race of sorts to create the quickest and most elegant solution possible for this 😃

---

<div class="post-metadata">

### Author: ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)
#### Post date: [November 2, 2023, 10:14pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/15 "2023-11-02T22:14:43Z")

</div>

Nice!

But isn’t the \<50% chance\> equivalent to something like \<pick\_random(0, 1) = 1\>, thus still having two random generations, like this?:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/543cf72fbc00cfd245ed6aead3bdc0aff8642009.png)

(pluss, the \<50% chance\> seems a bit slower than a \<pick\_random(0, 1) = 1\> for some unknown reason…) 😉

How about this (un)intuitive monster?:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/d6bb44493e5598a50b50603367c96f2dc4b49901.png)

You could drop the stop variable and range calculation, and operate instead only with start = 40 and range = 50 directly.

> [@Sarge](#):
>
> BTW, I like how this thread has become a race of sorts to create the quickest and most elegant solution possible for this 😃

Me too! There’s a special beauty to an algorithm developing into its optimal self, especially through joint effort!

Here’s a collection of most of the contributions so far, with a testing loop:  
[https://makecode.com/\_PLAL2RKL3ggd](https://makecode.com/_PLAL2RKL3ggd)

---

<div class="post-metadata">

### Author: ![CreativeUsername](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/creativeusername/32/9601_2.png) [@CreativeUsername](https://forum.makecode.com/u/CreativeUsername)
#### Post date: [November 2, 2023, 10:17pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/16 "2023-11-02T22:17:14Z")

</div>

![arcade-screenshot](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/4/47093eb483e5d7625b1ee3c530ea33f844cd5b2d.png)  
Here’s my take on this problem. It’s very similar to the other ones others have made, but I just wanted to post a more simple solution

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [November 2, 2023, 10:21pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/17 "2023-11-02T22:21:55Z")

</div>

Wait a minute, this is just the same thing as what @AlexK did, just a bit shuffled…  
I’m sorry y’all I guess I misread his algorithm, I’m sorry it was like 2AM idk what I was even thinking then 😅

---

<div class="post-metadata">

### Author: ![Don](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/don/32/17989_2.png) [@Don](https://forum.makecode.com/u/Don)
#### Post date: [November 6, 2023, 7:41pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/18 "2023-11-06T19:41:24Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/a/a9950fb645b03c494154f54f45026f28216067a0.png)

---

<div class="post-metadata">

### Author: ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)
#### Post date: [November 6, 2023, 10:24pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/19 "2023-11-06T22:24:47Z")

</div>

Beautiful!

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [November 7, 2023, 9:22pm UTC](https://forum.makecode.com/t/or-in-a-random-block/23798/20 "2023-11-07T21:22:45Z")

</div>

Pure math, eh?
