This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-30 19:01:12

Scatcycle
Scratcher
Registered: 2010-07-22
Posts: 17

How scratch does Random

You cannot truly achieve random. There always has to be hard coding involved. That's just how it is with computers. So I'm wondering what Scratch uses to achieve the closest thing to random?

Offline

 

#2 2011-10-30 19:09:57

laptop97
Scratcher
Registered: 2011-06-27
Posts: 1000+

Re: How scratch does Random

Green Flag
Set [Random Variable] to (pick random (1) to (5) )
If < (Random Variable) = (1) >
Say (Hello!)
{End of If}
If < (Random Variable) = (2) >
Say (Bye!)
{End of If}
If < (Random Variable) = (3) >
Say (A random statement!)
{End of If}
If < (Random Variable) = (4) >
Say (Ok!)
{End of If}
If < (Random Variable) = (5) >
Say (Nothing!)
{End of If}

=

[blocks]
<when green flag clicked>
<set{ Random Variable }to( <pick random( 1 )to( 5 )) )>
<if><( <{ Random Variable }> <=> 1 )>
<say[ Hello! ]
<end>
<if><( <{ Random Variable }> <=> 2 )>
<say[ Bye! ]
<end>
<if><( <{ Random Variable }> <=> 3 )>
<say[ A random statement! ]
<end>
<if><( <{ Random Variable }> <=> 4 )>
<say[ Ok! ]
<end>
<if><( <{ Random Variable }> <=> 5 )>
<say[ Nothing! ]
<end>
[/blocks]

Last edited by laptop97 (2011-10-30 19:16:35)

Offline

 

#3 2011-10-31 00:45:18

Scatcycle
Scratcher
Registered: 2010-07-22
Posts: 17

Re: How scratch does Random

No, I know how to use scratch. What I'm saying, is how do they get that random number? You can't just tell a computer to randomly pick a number 1-10. It just isn't possible.

Offline

 

#4 2011-10-31 09:33:02

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How scratch does Random

I don't know, but I'd guess it uses the clock and runs it through a random number algorithm.  For example, the milliseconds on the computer's clock says 76.  The computer runs it through a formula (for example: (2x+4)%10) to get 6.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2011-10-31 09:34:19

AIOlover
Scratcher
Registered: 2011-05-08
Posts: 100+

Re: How scratch does Random

Here is the scratch code for it:

randomFrom: t1 to: t2

| t3 t4 t5 |
t3 _ t1 min: t2.
t4 _ t1 max: t2.
t5 _ RandomGen next * (t4 - t3) + t3.
t3 isInteger & t4 isInteger
    ifTrue: [t5 _ (RandomGen next * (t4 + 1 - t3)) truncated + t3]
    ifFalse: [t5 _ RandomGen next * (t4 - t3) + t3].
^ t5


I'm currently http://blocks.scratchr.org/API.php?user=AIOlover&amp;action=onlineStatus&amp;type=text!
big_smile ~AIOlover~ big_smile

Offline

 

#6 2011-10-31 10:31:36

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: How scratch does Random

AIOlover wrote:

Here is the scratch code for it:

randomFrom: t1 to: t2

| t3 t4 t5 |
t3 _ t1 min: t2.
t4 _ t1 max: t2.
t5 _ RandomGen next * (t4 - t3) + t3.
t3 isInteger & t4 isInteger
    ifTrue: [t5 _ (RandomGen next * (t4 + 1 - t3)) truncated + t3]
    ifFalse: [t5 _ RandomGen next * (t4 - t3) + t3].
^ t5

this is only block method.
look at kernel-numbers/random

Offline

 

#7 2011-10-31 11:14:44

henley
Scratcher
Registered: 2008-06-21
Posts: 1000+

Re: How scratch does Random

It grabs a number from a deep decimal in the timer.


"I've worked so hard for you and you give me nothing in return. Do you need help... Or do I?"

Offline

 

#8 2011-10-31 11:15:46

henley
Scratcher
Registered: 2008-06-21
Posts: 1000+

Re: How scratch does Random

Like this:

342.972465978243675978236459872346635758964258962490

And which numbers depend on what the limits of the random number are.


"I've worked so hard for you and you give me nothing in return. Do you need help... Or do I?"

Offline

 

Board footer