Suppose one writes a project in which a Sprite's action is based on a random choice among four possibilities. Up to this point I've written that as a series of nested If's and If/Else statements. But it seemed to me those scripts didn't really work right.
My latest attempt ("Random Walk") has an inital statement setting a variable Choice to "random 1-4". And then the script states that if Choice = 1, "a" happens; if Choice = 2, "b"....and so on. http://scratch.mit.edu/projects/Larry828/701892
That way there's *one* randomized choice made and the consequences follow.
Is it possible that if one writes a series of nested IF statements (eg IF 1-4 = 1, then a; IF 1-4 = 2, then b, etc) that each IF statement actually makes a 'new' choice? If so, it seems logically possible to run through a series of statements like that and not have the right number chosen at the right time!
Any observations or links to successful projects would be appreciated!
Offline
I made a project that picks random numbers. http://scratch.mit.edu/projects/Magnie/695598 Just change Repeat to 4 and change (pick random number from ( 1 ) to ( 10 ) ) to (pick random from ( 1 ) to ( 4 ) ) And you should get the correct results.
Offline
Never mind, it won't work for what you want.
Offline
The way you made your script is exactly the way it should be done.
As for your last paragraph, I can't understand what you are asking. It would help to describe it in more detail, maybe show a buggy project?
Last edited by archmage (2009-10-02 14:16:41)
Offline
archmage wrote:
The way you made your script is exactly the way it should be done.
As for your last paragraph, I can't understand what you are asking. It would help to describe it in more detail, maybe show a buggy project?
Well, I'm embarrassed to point to this project but I've left it online precisely to illustrate the *wrong* way of doing it: http://scratch.mit.edu/projects/Larry828/541191. I've just now rewritten that as "Four Cats -v3" using my new method with variable. It's uploaded now too. I can see now that each line in the old project could indeed call the 'wrong' number randomly. In fact, listen carefully as you play the old project: if you hear a "meow" BEFORE a question is asked it means that the loop was executed *without* calling a question. Sometimes I've heard at least two "meows" before the question was called. That's what drove me to probe the logic of using randomization. It used to be worse! If the question wasn't called, the game said "Sorry" and sent one back to Stage 1. (!) _______ Thanks for confirming the accuracy of using a variable to capture the random choice!!
Offline