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

#1 2009-04-21 21:02:25

Puddingreg
Scratcher
Registered: 2008-06-11
Posts: 2

Randomization

I doubt this is a bug with scratch, but I'm desperate for help. So here's my situation.

I have a sprite that needs to have a totally random costume. What I've done is "When I receive (broadcast), if random 1-5=1, change to costume #1. If random 1-5=2, switch to costume #2, etc., etc. However, at #5, I say "If random 1-5=5", than I list Three different costumes that are randomly picked from, dividing the chances that any of these will be picked by three (that's a one in fifteen, if you're wondering). But, the project keeps on picking the last one. It may be because, for that one, I've added "If pick random 1-3=3, and (variable)<17, switch to costume 7, else broadcast (broadcast message from sentence #2). I can post it as an incomplete project is anybody thinks it will help

Thanks,
Puddingreg


gReg

Offline

 

#2 2009-04-21 21:05:28

Puddingreg
Scratcher
Registered: 2008-06-11
Posts: 2

Re: Randomization

Oh, and to add, the variable that is smaller than 17 isn't limiting it. It's as if it didn't exist

Please help
Puddingreg


gReg

Offline

 

#3 2009-04-22 05:17:25

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomization

There's probably a lot of ways to do what you want...I like to do it this way.  First, figure out what percentage of the time you want each costume to show...let's say you have 5 costumes and you want them to show in the following percentages: 5%, 30%, 20%, 44%, 1%

So, if you pick a random number from 1 to 100, then the ranges would be

Costume 1 - From 1 to 5
Costume 2 - From 6 to 35
Costume 3 - From 36 to 55
Costume 4 - From 56 to 99
Costume 5 - From 100 to 100

Notice that if you subtract the low end of each range from the top end and add one, you get the percentage that costume should be displayed.  Now the code to set up the ranges, using nested if-then-else blocks, is:

[blocks]
<set{  N  }to( <pick random( 1 )to( 100
<if><( N <<> 6  )>
<switch to costume[ 1
<else>
   <if><( N <<> 36  )>
   <switch to costume[ 2
   <else>
      <if><( N <<> 56  )>
      <switch to costume[ 3
      <else>
         <if><( N <<> 100  )>
         <switch to costume[ 4
         <else>
         <switch to costume[ 5
         <end>
      <end>
   <end>
<end>

[/blocks]


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#4 2009-04-22 09:13:24

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Randomization

Or:

[blocks]
<when green flag clicked>
<forever>
<switch to costume[ <pick random( 1 )to( 10 )) )>
<end>
[/blocks]

Last edited by Magnie (2009-04-22 09:14:13)

Offline

 

#5 2009-04-22 16:31:37

bosox397
Scratcher
Registered: 2008-02-17
Posts: 1000+

Re: Randomization

Magnie wrote:

Or:

[blocks]
<when green flag clicked>
<forever>
<switch to costume[ <pick random( 1 )to( 10 )) )>
<end>
[/blocks]

i dont think that would help


Dear Scratch Users,
I'm done with scratch, or at least making projects. I have made one last big game, thats both fun and teaches a lesson about water. It'd mean a lot if you gave me feedback.                              http://scratch.mit.edu/projects/bosox397/569201

Offline

 

#6 2009-04-22 16:47:26

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomization

Magnie wrote:

Or:

[blocks]
<when green flag clicked>
<forever>
<switch to costume[ <pick random( 1 )to( 10 )) )>
<end>
[/blocks]

Your approach is fine if you are looking for each costume to be picked the same amount of times (on average).  I was trying to describe a technique that allows you to specify unequal (and very customizable) costume usage.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#7 2009-04-22 20:43:05

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Randomization

Okay.  smile

Offline

 

#8 2009-04-22 20:46:15

ScratchMusicGroup
Scratcher
Registered: 2009-04-12
Posts: 100+

Re: Randomization

hmm that helps me too!


http://i44.tinypic.com/2yjx4ht.jpg                  http://i44.tinypic.com/xljspe.png        http://i40.tinypic.com/16c3rdi.png        http://i39.tinypic.com/16jowol.png

Offline

 

Board footer