Discuss Scratch

varadyjc
Scratcher
56 posts

How the "Pick Random" Block Works

My question is quite simple. How does the
(pick random (1) to (10))
block work? How does it decide what number to choose?

Any answer is appreciated.

Last edited by varadyjc (Sept. 4, 2016 16:18:18)

varadyjc
Scratcher
56 posts

How the "Pick Random" Block Works

I think it might have to do applying several calculations to the current time to produce a near-random number.

Last edited by varadyjc (Sept. 4, 2016 16:19:29)

jokebookservice1
Scratcher
1000+ posts

How the "Pick Random" Block Works

varadyjc wrote:

I think it might have to do applying several calculations to the current time to produce a near-random number.
Yes, it has to be predetermined, it just looks random. These random numbers are called pseudo-random numbers.

Scratch uses Math.random so this might hep?
-Io-
Scratcher
1000+ posts

How the "Pick Random" Block Works

varadyjc wrote:

I think it might have to do applying several calculations to the current time to produce a near-random number.
As far as I know, that's it
Scratch uses AS3's Math.random. It's a pseudo-random generator and thus it needs a seed which is most of the times just the current time.

EDIT: ninja'd :p

Last edited by -Io- (Sept. 4, 2016 17:08:02)


varadyjc
Scratcher
56 posts

How the "Pick Random" Block Works

-Io- wrote:

varadyjc wrote:

I think it might have to do applying several calculations to the current time to produce a near-random number.
As far as I know, that's it
Scratch uses AS3's Math.random. It's a pseudo-random generator and thus it needs a seed which is most of the times just the current time.

EDIT: ninja'd :p
Thank you!
varadyjc
Scratcher
56 posts

How the "Pick Random" Block Works

jokebookservice1 wrote:

varadyjc wrote:

I think it might have to do applying several calculations to the current time to produce a near-random number.
Yes, it has to be predetermined, it just looks random. These random numbers are called pseudo-random numbers.

Scratch uses Math.random so this might hep?
Thank you! That's helpful!
TheLogFather
Scratcher
1000+ posts

How the "Pick Random" Block Works

There are some oddities about it that you might want to be aware of…

https://scratch.mit.edu/projects/96080245/

In particular, note that it has a resolution of 1 in 2^31. That means if you “pick random (0.0) to (1.0)”, which picks a random real number from zero to one, and then multiply it by 2^31, it will always end up with an integer value.

Also, note that Scratch's “pick random” block is meant to give an integer result if both entries are integers. But if either is written as a real number (e.g. “1.0” or “0.0”, as above) then it will give a real value (with the 1 in 2^31 resolution).

However, there's a glitch in the way values are saved which means that entries like “1.0” and “0.0” (i.e. something which is written as a real value, but is equal to an integer) end being turned into integer values when the project is next loaded. (So “0.0” becomes “0” and “1.0” becomes “1”). This means “pick random 0.0 to 1.0” will give real values before you save the project, and get transformed into “pick random 0 to 1” on reloading, so it then only gives either zero or one

Hope those are helpful!

Last edited by TheLogFather (Sept. 4, 2016 21:27:22)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

IcyCoder
Scratcher
1000+ posts

How the "Pick Random" Block Works

How can you make a pick random block alternitive

Because JS is the future (echos) future future futur futu fut fu f
Jonathan50
Scratcher
1000+ posts

How the "Pick Random" Block Works

IcyCoder wrote:

How can you make a pick random block alternitive
Use one of these algorithms.

Not yet a Knight of the Mu Calculus.
scratchisthebest
Scratcher
1000+ posts

How the "Pick Random" Block Works

TheLogFather wrote:

However, there's a glitch in the way values are saved which means that entries like “1.0” and “0.0” (i.e. something which is written as a real value, but is equal to an integer) end being turned into integer values when the project is next loaded. (So “0.0” becomes “0” and “1.0” becomes “1”). This means “pick random 0.0 to 1.0” will give real values before you save the project, and get transformed into “pick random 0 to 1” on reloading, so it then only gives either zero or one
Are there ways around this bug?

One way I can think of is “pick random 0 to 1.000000000001”, but I'm sure there's a smarter solution right?

I am a Lava Expert
IcyCoder
Scratcher
1000+ posts

How the "Pick Random" Block Works

Question: in this situation is there really a 50% chance of it equaling one?

(pick random (1) to (2))

Because JS is the future (echos) future future futur futu fut fu f
TheLogFather
Scratcher
1000+ posts

How the "Pick Random" Block Works

scratchisthebest wrote:

Are there ways around this bug?
One way I can think of is “pick random 0 to 1.000000000001”, but I'm sure there's a smarter solution right?
I replied:
That's pretty much what I've done with it…

Unfortunately, you can't even use tricks such as “join” to turn it into a string (e.g. "join [1.] [0]“, or something), ‘cos Scratch converts it into a numerical value (since it knows that’s the type of the input), so it then ends up as just the value 1, which it then considers to be an integer.

For ”0.0“, I do something like ”1e-50".
I just realised that, on my ‘pick random investigations’ project, @asivi pointed me to a project that puts a single dot into the first slot. Scratch translates that into a zero, but with two useful properties:
1) It contains a decimal point, so Scratch treats it as a real number, meaning “pick random” will return a real number;
2) Scratch does *save* it as a dot, so it gets reloaded as a dot and will therefore continue to work across reloads.

That means you can easily create a “pick random from (first) to (last)” that works for real numbers:
( (pick random (.) to ((last)-(first))) + (first) ) // The dot gets translated as "0.0" but is kept across reloads.

Last edited by TheLogFather (Sept. 5, 2016 14:56:41)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

Dylan5797
Scratcher
1000+ posts

How the "Pick Random" Block Works

IcyCoder wrote:

How can you make a pick random block alternitive
Implement a pseudorandom algorithm in scratch (which would be huge and a waste of time to make)

TheLogFather
Scratcher
1000+ posts

How the "Pick Random" Block Works

Woah…! Did I really just EDIT my post above rather than actually quote it to post a new reply…

…and now I can't remember all that it originally said, so all that's left is the bit in there that I quoted from it…

Last edited by TheLogFather (Sept. 5, 2016 15:01:58)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

wizzwizz7
Scratcher
500+ posts

How the "Pick Random" Block Works

scratchisthebest wrote:

TheLogFather wrote:

However, there's a glitch in the way values are saved which means that entries like “1.0” and “0.0” (i.e. something which is written as a real value, but is equal to an integer) end being turned into integer values when the project is next loaded. (So “0.0” becomes “0” and “1.0” becomes “1”). This means “pick random 0.0 to 1.0” will give real values before you save the project, and get transformed into “pick random 0 to 1” on reloading, so it then only gives either zero or one
Are there ways around this bug?

One way I can think of is “pick random 0 to 1.000000000001”, but I'm sure there's a smarter solution right?
pick random (0) to (join[1][.0])



CodeLegend
Scratcher
500+ posts

How the "Pick Random" Block Works

TheLogFather wrote:

However, there's a glitch in the way values are saved which means that entries like “1.0” and “0.0” (i.e. something which is written as a real value, but is equal to an integer) end being turned into integer values when the project is next loaded. (So “0.0” becomes “0” and “1.0” becomes “1”). This means “pick random 0.0 to 1.0” will give real values before you save the project, and get transformed into “pick random 0 to 1” on reloading, so it then only gives either zero or one
Obfuscated remix detection, anyone?
PullJosh
Scratcher
1000+ posts

How the "Pick Random" Block Works

CodeLegend wrote:

Obfuscated remix detection, anyone?
Oooh, that's clever. I'm not a big fan of actually preventing remixes on projects (remixing is a fantastic way to learn), but it would be fun to play around with just to see if it's possible.
CodeLegend
Scratcher
500+ posts

How the "Pick Random" Block Works

PullJosh wrote:

CodeLegend wrote:

Obfuscated remix detection, anyone?
Oooh, that's clever. I'm not a big fan of actually preventing remixes on projects (remixing is a fantastic way to learn), but it would be fun to play around with just to see if it's possible.
Same here. I wonder if this would work when someone remixes without making any edits?
IcyCoder
Scratcher
1000+ posts

How the "Pick Random" Block Works

CodeLegend wrote:

PullJosh wrote:

CodeLegend wrote:

Obfuscated remix detection, anyone?
Oooh, that's clever. I'm not a big fan of actually preventing remixes on projects (remixing is a fantastic way to learn), but it would be fun to play around with just to see if it's possible.
Same here. I wonder if this would work when someone remixes without making any edits?
hmm really cool idea.

Because JS is the future (echos) future future futur futu fut fu f

Powered by DjangoBB