2014년 12월 29일 월요일

Reducing Numbers Logic ?

Having a post Christmas brain malfunction!

To my mind this should work, but i am still getting duplicate numbers out of it. What i am seeking to achieve is a different number between 1 and 100 on each click of the button, the code should prevent previously selected numbers from appearing in the label text again. But they do. What have I missed ? ( It must be something simple!)  (Variables set to 0 so that the first run, using 0 gets excluded)




"if false" always is false, which means always the else part will be executed and never the then part...



Of course :D

I'll try with the "if thing in list" test from the while loop?



Turned the logic around and tidied up things to prevent running out of numbers. Still get the occasional repeated number in the text box though....




It's simpler to keep a shrinking list of numbers that HAVEN'T been selected yet.
Start with all the numbers from 1 to 100,
pick a random index in that list,
copy the number at that index, then drop that index from the list.
The list will shrink, but you will never have to re-try selecting
from the list because of duplication.



Yes ABG, have done it that way before and it works well, seeking to avoid having to create long lists by hand. Possible to programmatically create a list 1-100 ? For Example ?




There's a FOR RANGE block you could use instead.
It would save a block and it would be repeatable

(self-initializing).



See attached, showing how to fill the list.




Nice :)


댓글 없음:

댓글 쓰기