Discuss Scratch

zvido
Scratcher
2 posts

Random number but not the previous one

Hi,

I am trying to create a script in which a random number between 1 and 4 is generated. Based on the number generated certain things will happen. Then I want the program to select another random number between 1 and 4 but not the same one that was previously used until all four numbers are selected. The script seems to work well for the first three rounds but then it sometimes works on the fourth round and sometimes it stops. I have no idea why. (I put in a meow sound for debugging purposes, sometimes I hear it and the script loops around and at others no meow and the program stops.) Any ideas on how to make this work all the time would be greatly appreciated. Alternative ideas for achieving the same result are also great.

Here is a link to screen shot of the script.

http://torahskills.org/tsresources/images/jfs/scratch.png

Last edited by zvido (Aug. 19, 2013 20:19:42)

CrystalStar-
Scratcher
100+ posts

Random number but not the previous one

As you never reset B1- 4, maybe 4 is already set to a good number?

Try setting all the variables to 0 every time you start a completely new round at 1.

CrystalStar-

Play Scramble here!
DadOfMrLog
Scratcher
1000+ posts

Random number but not the previous one

The reason it gets stuck is because you have “or (b2=B1)” in the final “if” (should be b4 rather than b2).
(BTW, why do you need those “ifs” anyway? If it's still within the repeat loop then the conditions must be true…?)

However, here's an alternative suggestion how to do this, in a more generic way (that you can expand in the future)…

How about putting numbers 1 to N into a list (N=4 in your example), and then repeat the following N times: pick a random from 1 to length of list, get that item (maybe add into another list), and then delete the item that was picked.

So something like this (hope I get this right - even if not, hopefully you'll get the idea):

delete all of numsToPick
set pos to 0
repeat N
change pos by 1
add pos to numsToPick
delete all of numsPicked
repeat N
set pos to (pick random 1 to length of numsToPick)
add (item pos of numsToPick) to numsPicked
delete (pos) of numsToPick

Now you have a list, numsPicked, which contains the numbers 1 to N in a random order, and you can do whatever you want with them…

Hope that helps!

EDIT: you could even define a custom block for above: “randomize values (start) to (end)”, say - in that case, you'd replace N with “end-start+1” and you'd start by setting pos to “start-1” rather than “0” - this way means you don't necessarily have to have the set of numbers going from 1 to N, but from “start” to “end”.
Now you can just drop this custom block wherever you want to generate a randomly ordered list of non-repeating integers.

Last edited by DadOfMrLog (Aug. 19, 2013 21:57:03)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

zvido
Scratcher
2 posts

Random number but not the previous one

Thank you both for taking the time to reply.

CrystalStar, I did try your idea but that did not seem to help. Thanks again for trying.

DadofMrLog, you are right there was a typo in the bottom line and changing b4 to b2 did the trick (can't believe I missed that). Also, you are right, I do not need the IF statements. I am new to Scratch and am just getting my feet wet.

I tried following your other suggestion but got lost along the way. I am sure others will find it useful and maybe I will understand it as I get more familiar with Scratch. For now, I am hoping this script should do the trick.

The most important thing I learned was the Scratch board/community is great!!

Thanks folks!

zvido

Last edited by zvido (Aug. 19, 2013 22:31:03)

jsboygenius
Scratcher
28 posts

Random number but not the previous one

Nevermind.

Last edited by jsboygenius (Aug. 19, 2013 22:36:16)


<– Click

Check out my latest game, TANKS: Command, currently in Beta!
NZombeh
Scratcher
100+ posts

Random number but not the previous one

How about…
If = 1
Broadcast 1

When I recieve 1
Choose random 2,3,4

Do you get what I mean?


birchyboii
Scratcher
100+ posts

Random number but not the previous one

Thats actually a nice idea NZombeh its simple, the only downfall is there would be alot of unnecessary scripts as you would have to create broadcasts for every combination
NZombeh
Scratcher
100+ posts

Random number but not the previous one

birchyboii wrote:

Thats actually a nice idea NZombeh its simple, the only downfall is there would be alot of unnecessary scripts as you would have to create broadcasts for every combination
Thanks, and yeah, that's what I was thinking.


goodgames8376
Scratcher
8 posts

Random number but not the previous one

http://scratch.mit.edu/projects/21934190/ Is a times table game I made and it never repeats the same number.I hope it helps!
digthebone
Scratcher
500+ posts

Random number but not the previous one

say (pick random (1) to (4)) for (2) secs

or if you want it to set a variable to a number 1 through 4:
set [random variable v] to (pick random (1) to (4))

or if you want it to move 1 to 4 steps
move (pick random (1) to (4)) steps

get the idea?Hope you did
forever

say [ok buddy u awesome ] for (2) secs
if <person hates you> then
say [ok buddy u bad bro] for (2) secs

end
end

Digthebone

I make stuff. And I am always digging for new idea's.




scratchmouse
Scratcher
70 posts

Random number but not the previous one

goodgames8376 wrote:

http://scratch.mit.edu/projects/21934190/ Is a times table game I made and it never repeats the same number.I hope it helps!
Hi, I like your idea very much and I have decided to make a visualization in which the sprite's speed depends on the (remaining) current distance, in my case Y-difference only, from the coordinates the sprite is moving towards. I created new block to implement the idea inside this project http://scratch.mit.edu/projects/24510942 It is not finished yet, as I want to add the “follow through” (and oscillation) idea which is described in this Wikipedia article here (that I edited when exploring the animation basics).

˙˙˙ ˙˙ ˙Ignore˙ ˙˙ ˙˙˙

… .. ˇˇˇ ˇˇˇ ˇˇˇ ˇˇˇ .. …
::: :: … ˇˇˇ ˇˇˇ … :: :::
coxy5
Scratcher
100+ posts

Random number but not the previous one

I am not sure if this was solved or not. I think it was but the code used looks complicated. I would use a list to solve this problem.

when green flag clicked
set [i v] to [0]
repeat (4)
change [i v] by (1)
insert (i) at (random v) of [numbers v]
end

You then have a list of 1-4 in a random order.

Simply use:
(item (1 v) of [numbers  v])
(item (2 v) of [numbers v])
(item (3 v) of [numbers v])
(item (4 v) of [numbers v])

To use the numbers.

Last edited by coxy5 (July 11, 2014 10:47:02)

RandomPerson1789
Scratcher
100+ posts

Random number but not the previous one

set [a v] to (pick random (1) to (4)
set [b v] to (pick random (1) to (4)
repeat until <not <(b) = (a)>>
set [b v] to (pick random (1) to (4)
end
There is another way, but this is the easy way.
Hope this helps.
Harakou
Scratch Team
1000+ posts

Random number but not the previous one

I'm afraid this topic is rather old and it seems the OP is satisfied with the answers they've received - closing to prevent any more unnecessary bumping.

My signature has gone into hiding.

Powered by DjangoBB

Standard | Mobile