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

#1 2013-01-28 03:35:12

benSeath
New Scratcher
Registered: 2013-01-28
Posts: 1

Random operator

Hey guys,

I'm new to Scratch and was wondering if anyone can help me out!

After talking to my teacher, I decided to take up his challenge of "making" a ticket vendor in Scratch. One of his challenges was for me to give "customers" a discount of minus $1 if they purchase a ticket on a weekend. I can't seem to figure this out. His hint was that I needed to use thr random operator.

Thanks guys

Offline

 

#2 2013-01-28 12:08:53

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Random operator

hmmm.... Random operator huh?
well, i am only guessing, but the first thing to do is to build your week, and then have the weekend day's send a message to the ticket dispenser that it's a weekday.
I think he may have been saying to use the random operator for determining the day.

If you want me to, i can try to make a program that does that...  If you do then just post it, then if i am successful i will post it and put the link here. Then you can download it and i can delete it off my projects.

so just to clarify, you need to either make 7 sprites with text that represents each day, or make one sprite that has 7 costumes and then make it so that if a variable called "day_" is a certain number, then go to the corresponding costume or sprite.

I hope that helps,

CAA14

Offline

 

#3 2013-01-28 13:42:22

dechan
Scratcher
Registered: 2012-12-20
Posts: 69

Re: Random operator

benSeath wrote:

Hey guys,

I'm new to Scratch and was wondering if anyone can help me out!

After talking to my teacher, I decided to take up his challenge of "making" a ticket vendor in Scratch. One of his challenges was for me to give "customers" a discount of minus $1 if they purchase a ticket on a weekend. I can't seem to figure this out. His hint was that I needed to use thr random operator.

Thanks guys

Here's the "algorithm" your teacher may have been talking about.

Create a list of Days, this will associate a day with a number.
set a variable daynumber with a range from 1 to the length of Days
Then test to see if Day is Saturday or Sunday and use an If/else block to assign a value to Discount.

add [Monday] to Days
add [Tuesday] to Days
add [Wednesday] to Days
add [Thursday] to Days
add [Friday] to Days
add [Saturday] to Days
add [Sunday] to Days
set [daynumber v] to (pick random (1) to (length of [Days]))
if < < ( daynumber ) = [6]  > or  < ( daynumber ) = [7]  > >
  set [Discount v] to [1]
else
  set [Discount v] to [0]
end

Last edited by dechan (2013-01-28 18:42:10)

Offline

 

#4 2013-01-28 13:44:26

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Random operator

Hi,

Good algorithm,

I would of done it different because i don't yet know how to use lists.

Regards,

CAA14

Offline

 

#5 2013-02-09 09:39:40

cool_dude_2
Scratcher
Registered: 2012-12-05
Posts: 82

Re: Random operator

random operator is

(pick random (#) to (#))
because it falls under operators category and is random!


http://blocks.scratchr.org/API.php?user=cool_dude_2&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1i am http://blocks.scratchr.org/API.php?user=cool_dude_2&amp;action=onlineStatus&amp;type=text

Offline

 

#6 2013-02-09 09:46:18

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Random operator

dechan wrote:

benSeath wrote:

Hey guys,

I'm new to Scratch and was wondering if anyone can help me out!

After talking to my teacher, I decided to take up his challenge of "making" a ticket vendor in Scratch. One of his challenges was for me to give "customers" a discount of minus $1 if they purchase a ticket on a weekend. I can't seem to figure this out. His hint was that I needed to use thr random operator.

Thanks guys

Here's the "algorithm" your teacher may have been talking about.

Create a list of Days, this will associate a day with a number.
set a variable daynumber with a range from 1 to the length of Days
Then test to see if Day is Saturday or Sunday and use an If/else block to assign a value to Discount.

add [Monday] to Days
add [Tuesday] to Days
add [Wednesday] to Days
add [Thursday] to Days
add [Friday] to Days
add [Saturday] to Days
add [Sunday] to Days
set [daynumber v] to (pick random (1) to (length of [Days]))
if < < ( daynumber ) = [6]  > or  < ( daynumber ) = [7]  > >
  set [Discount v] to [1]
else
  set [Discount v] to [0]
end

Why create the list when it will never be used?  hmm  If the list is static and you want to get the length, why not just specify the length?

Here, use this:

set [day v] to (pick random (1) to (7))
set [discount v] to [0]
if <(day) > [5]>
  set [Discount v] to [1]
end

Offline

 

#7 2013-02-09 10:40:23

dechan
Scratcher
Registered: 2012-12-20
Posts: 69

Re: Random operator

Gravitation wrote:

dechan wrote:

benSeath wrote:

Hey guys,

I'm new to Scratch and was wondering if anyone can help me out!

After talking to my teacher, I decided to take up his challenge of "making" a ticket vendor in Scratch. One of his challenges was for me to give "customers" a discount of minus $1 if they purchase a ticket on a weekend. I can't seem to figure this out. His hint was that I needed to use thr random operator.

Thanks guys

Here's the "algorithm" your teacher may have been talking about.

Create a list of Days, this will associate a day with a number.
set a variable daynumber with a range from 1 to the length of Days
Then test to see if Day is Saturday or Sunday and use an If/else block to assign a value to Discount.

add [Monday] to Days
add [Tuesday] to Days
add [Wednesday] to Days
add [Thursday] to Days
add [Friday] to Days
add [Saturday] to Days
add [Sunday] to Days
set [daynumber v] to (pick random (1) to (length of [Days]))
if < < ( daynumber ) = [6]  > or  < ( daynumber ) = [7]  > >
  set [Discount v] to [1]
else
  set [Discount v] to [0]
end

Why create the list when it will never be used?  hmm  If the list is static and you want to get the length, why not just specify the length?

Here, use this:

set [day v] to (pick random (1) to (7))
set [discount v] to [0]
if <(day) > [5]>
  set [Discount v] to [1]
end

Specifying a list of constants with a self-generated numerical index created by a list is good solid programming.
If someone is going to start using Lists in Scratch this is one of the better ways to get started.

If I was teaching the class and someone brought me a solution which used a list they would get a "+" because instead of creating some elaborate "if" blockset to display a value they could use

say <item [Day v] of [Days v]> 
which is much better than using a large set of if-blocks, and any time we can eliminate if blocks we're winning.

Offline

 

#8 2013-02-09 18:40:36

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Random operator

CAA14 wrote:

Hi,

Good algorithm,

I would of done it different because i don't yet know how to use lists.

Regards,

CAA14

I could teach you how to use lists if you would like.

Last edited by ImagineIt (2013-02-10 11:05:54)

Offline

 

#9 2013-02-09 19:33:35

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Random operator

dechan wrote:

benSeath wrote:

Hey guys,

I'm new to Scratch and was wondering if anyone can help me out!

After talking to my teacher, I decided to take up his challenge of "making" a ticket vendor in Scratch. One of his challenges was for me to give "customers" a discount of minus $1 if they purchase a ticket on a weekend. I can't seem to figure this out. His hint was that I needed to use thr random operator.

Thanks guys

Here's the "algorithm" your teacher may have been talking about.

add [Monday] to [Days v]
add [Tuesday] to [Days v]
add [Wednesday] to [Days v]
add [Thursday] to [Days v]
add [Friday] to [Days v]
add [Saturday] to [Days v]
add [Sunday] to [Days v]
set [daynumber v] to (pick random (1) to (length of [Days]))
if < < ( daynumber ) = [6]  > or  < ( daynumber ) = [7]  > >
  set [Discount v] to [1]
else
  set [Discount v] to [0]
end

Fixed.


http://i46.tinypic.com/35ismmc.png

Offline

 

#10 2013-02-09 19:46:59

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Random operator

dechan wrote:

Gravitation wrote:

dechan wrote:


Here's the "algorithm" your teacher may have been talking about.

Create a list of Days, this will associate a day with a number.
set a variable daynumber with a range from 1 to the length of Days
Then test to see if Day is Saturday or Sunday and use an If/else block to assign a value to Discount.

add [Monday] to Days
add [Tuesday] to Days
add [Wednesday] to Days
add [Thursday] to Days
add [Friday] to Days
add [Saturday] to Days
add [Sunday] to Days
set [daynumber v] to (pick random (1) to (length of [Days]))
if < < ( daynumber ) = [6]  > or  < ( daynumber ) = [7]  > >
  set [Discount v] to [1]
else
  set [Discount v] to [0]
end

Why create the list when it will never be used?  hmm  If the list is static and you want to get the length, why not just specify the length?

Here, use this:

set [day v] to (pick random (1) to (7))
set [discount v] to [0]
if <(day) > [5]>
  set [Discount v] to [1]
end

Specifying a list of constants with a self-generated numerical index created by a list is good solid programming.
If someone is going to start using Lists in Scratch this is one of the better ways to get started.

If I was teaching the class and someone brought me a solution which used a list they would get a "+" because instead of creating some elaborate "if" blockset to display a value they could use

say <item [Day v] of [Days v]> 
which is much better than using a large set of if-blocks, and any time we can eliminate if blocks we're winning.

However, it's not good to create needless arrays that take up memory, later in programming.

He never said to use lists/arrays.  I always try to use the simplest solution possible.  Not using the list is the simplest solution.

Personally, what I would do, not looking at any other solution, in pseudocode, is this:

Set Day to random from 1 to 7
Set price to 10
if Day = 6 or Day = 7 (weekends)
     change price by -1

That's how I would do it.  Only 4 lines of code.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#11 2013-02-09 20:59:07

dechan
Scratcher
Registered: 2012-12-20
Posts: 69

Re: Random operator

"For every problem there is a solution which is simple, clean and wrong.” Henry Louis Mencken

Anyone can make a case for using the simplest solution possible.
I provided an elegant easy to understand and easily extensible solution.
If someone finds what I did too complex please understand that I'm not a judgmental person, but people who read weak excuses for not using good programming practices may not be as enlightened as I am.  big_smile

Offline

 

#12 2013-02-10 11:07:58

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Random operator

dechan wrote:

"For every problem there is a solution which is simple, clean and wrong.” Henry Louis Mencken

Anyone can make a case for using the simplest solution possible.
I provided an elegant easy to understand and easily extensible solution.
If someone finds what I did too complex please understand that I'm not a judgmental person, but people who read weak excuses for not using good programming practices may not be as enlightened as I am.  big_smile

But your algorithm isn't extremely good either. There's no point in making a list/array that will never be used. It will just take up space.

Offline

 

#13 2013-02-10 14:43:47

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Random operator

ImagineIt wrote:

dechan wrote:

"For every problem there is a solution which is simple, clean and wrong.” Henry Louis Mencken

Anyone can make a case for using the simplest solution possible.
I provided an elegant easy to understand and easily extensible solution.
If someone finds what I did too complex please understand that I'm not a judgmental person, but people who read weak excuses for not using good programming practices may not be as enlightened as I am.  big_smile

But your algorithm isn't extremely good either. There's no point in making a list/array that will never be used. It will just take up space.

I just want to say that although you both have good points, please don't turn this into a flame war.


http://i46.tinypic.com/35ismmc.png

Offline

 

Board footer