Skip to first unread message

trev park

unread,
Feb 2, 2017, 7:02:32 AM2/2/17
to MIT App Inventor Forum
I am trying to create a screen that when a button is pressed 6 random numbers are generated.
numbers 1 to 5 must be between 1 and 39
number 6 must be between 1 and 14.

I can generate the numbers fine but the problem arises when I try to get to check that no two numbers are identical.
I have attached a copy of my blocks that I set up to do this yet I am still producing numbers that match, any help or advice as to what I am doing wrong would be appreciated.

blocks.png

Italo

unread,
Feb 2, 2017, 7:52:48 AM2/2/17
to MIT App Inventor Forum
Put the numbers in a list. Pick one from the list and delete that number so the next pick won't be able to pick that number again.

trev park

unread,
Feb 2, 2017, 7:57:41 AM2/2/17
to MIT App Inventor Forum
And how would I do that?
This is my first project

Italo

unread,
Feb 2, 2017, 8:16:52 AM2/2/17
to MIT App Inventor Forum
Then you should be doing some tutorials first. Search the forum for "how to work with lists".

Read http://appinventor.mit.edu/explore/ai2/support/blocks/lists.html#pickrandomitem

Also there's a tutorial about this same topic by Taifun. Search the forum for "how to pick a list item once".

Italo

unread,
Feb 2, 2017, 8:19:31 AM2/2/17
to MIT App Inventor Forum
This is the link for that tutorial: https://puravidaapps.com/snippets.php#2removerandom

Juan Antonio

unread,
Feb 2, 2017, 1:53:20 PM2/2/17
to MIT App Inventor Forum
Hi trev park, this aia code show in Label1 six numbers,  1 to 49, without repetition.







trev park

unread,
Feb 2, 2017, 6:05:50 PM2/2/17
to mitappinv...@googlegroups.com
Thankyou for posting that tutorial Italo, I followed it and got it to do what I wanted, now I am just researching how to resolve an odd error

Select list item: Attempt to get item number 1 of a list of length 0: ()
Note: You will not see another error reported for 5 seconds.

Im presuming that after pressing the button so many times it eventually as used up all the numbers in the list, I am trying to figure out that after the six numbers have been selected the next time the button is pressed all 49 numbers are then available again to choose from.
I may be wrong but I presume that the part:
initiallize global TB1to5 to list from csv row text"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39"
is actually outside of the when roll.click block

Taifun

unread,
Feb 2, 2017, 7:02:35 PM2/2/17
to MIT App Inventor Forum
It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

Taifun

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun. 

trev park

unread,
Feb 2, 2017, 7:09:50 PM2/2/17
to mitappinv...@googlegroups.com
What I am trying to do is generate six numbers every time the button ROLL is pressed with the following conditions:
the first five must be between 1 to 49
the sixth number must be between 1 and 14
none of these six numbers can match any other number
all numbers must be available when the ROLL button is pressed 

blocks (2).png

Italo

unread,
Feb 3, 2017, 12:36:13 AM2/3/17
to MIT App Inventor Forum

You are correct. You should reset the list every time the button is pressed.
Also, I think this is not right:


trev park

unread,
Feb 3, 2017, 7:58:32 AM2/3/17
to MIT App Inventor Forum
Yeah I noticed that extra block that you highlighted once i had uploaded the pic, But how do I reset the list after the button is pressed, I have tried all options I can think of and no avail

Taifun

unread,
Feb 3, 2017, 8:44:06 AM2/3/17
to MIT App Inventor Forum
in the definition set both lists to empty list
and in the button click event set the list elements

trev park

unread,
Feb 3, 2017, 9:03:14 AM2/3/17
to mitappinv...@googlegroups.com
Like this?

After some testing with what you suggested I think that swapping those blocks around seems to work well
Image3.png

trev park

unread,
Feb 4, 2017, 9:49:41 AM2/4/17
to mitappinv...@googlegroups.com
OK after testing and looking at results produced etc I have noticed the following:
use of the LENGTH OF LIST block returns how long the list is, so after an item is removed from the list the list length gets smaller

I am revising how I want this to happen.
1) It starts with an empty list
2) when button is clicked a list is populated with numbers
3) Pick at item from the list
4) display the value of that item in a label
5) remove that value from the list so that it can not be selected again (remove the actual value not its location in the list)


Imagine it like the game DEAL OR NO DEAL, lots of boxes (the numbers in the list) a box is removed and the contents displayed (points 3 and 4) and then the box is gone (point 5) leaving a the remaining boxes with there values but removing the box number (index location)
blocks.png

Taifun

unread,
Feb 4, 2017, 10:27:30 AM2/4/17
to MIT App Inventor Forum
use of the LENGTH OF LIST block returns how long the list is, so after am item is removed from the list the list length gets smaller
sure, the length of the list will give you always the correct length of the list
in case you still have issues, It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

trev park

unread,
Feb 4, 2017, 12:56:45 PM2/4/17
to mitappinv...@googlegroups.com
OK so this is what I want to happen

1) create empty list called lottolist
2) set what the list contains
3) pick a random item from that list
4) display what value was chosen in a label
5) remove that item from the list so that particular unique item can NOT be chosen again until I click the ROLL button

6) not have stupid error's appearing telling me that a certain index in the list does not exist and therefore can not be removed
blocks.png

Italo

unread,
Feb 4, 2017, 2:34:05 PM2/4/17
to MIT App Inventor Forum
Error messages are not stupid. They just tell you that what you are doing is wrong.
Did you try using the PICK A RANDOM ITEM block from the LISTS blocks? Please read the documentation links I gave you before and try to come up with something.

Taifun

unread,
Feb 4, 2017, 3:39:06 PM2/4/17
to MIT App Inventor Forum
there might be a misunderstanding what the difference is between an index and a value
let's assume, there are only 10 items in the list

index value
1       1
2       2
3       3
4       4
5       5
6       6 
7       7
8       8
9       9
10     10

now let's assume, you are picking the 5th item: picked item = 5
the new list looks like this (there are only 9 items in the list now)

index value
1       1
2       2
3       3
4       4
5       6
6       7 
7       8
8       9
9       10

Taifun

trev park

unread,
Feb 4, 2017, 6:51:41 PM2/4/17
to MIT App Inventor Forum
Yes Taifun, that is what is happening the remove from list statement removes the *th item from the list not the value of the item, I either need to work a way to put that value in a label before the index is deleted, I think I may have managed a work around for this but I will need to test more to find out if it worked

trev park

unread,
Feb 11, 2017, 9:31:45 AM2/11/17
to MIT App Inventor Forum
One more question relating to lists etc 
If I have a list called 'list' that contains 1,2,3,4,5,6,7,8,9,10 (having a list length of 10)
and then  I use the command 'pick a random item' from 'list' (lets say it picks item 5, the 5th index in the list)
it then stores this in a variable called selected (displaying this in a label would show a 5)
and then I use the command 'remove item from list' from list 'list' with index 'selected'
Am I correct in assuming that the list 'list' is then auto recalculated and would then contain the following 1,2,3,4,6,7,8,9,10 (noting that index 5 now contains the value 6) and the list length is now 9
if I am correct in assuming this, then if I pick another random item (lets say it picks index 5 again) would it return the value of 6 or 0 (0 meaning not in list)


Taifun

unread,
Feb 11, 2017, 10:20:44 AM2/11/17
to mitappinv...@googlegroups.com
Am I correct in assuming that the list 'list' is then auto recalculated and would then contain the following 1,2,3,4,6,7,8,9,10 
yes

if I am correct in assuming this, then if I pick another random item (lets say it picks index 5 again) would it return the value of 6
yes

Taifun 

trev park

unread,
Feb 11, 2017, 11:12:56 AM2/11/17
to mitappinv...@googlegroups.com

These blocks do this


Remove list item: Attempt to remove item 0 of the list (3 5 6 7 8 9 10). The minimum valid item number is 1.
blocks (1).png
Screenshot_2017-02-11-16-32-46[1].png

Taifun

unread,
Feb 11, 2017, 12:39:16 PM2/11/17
to MIT App Inventor Forum
you still don't understand the difference between index and value...
selectedball is the index and not the value! you should rename selectedball to index!
then use the select list item block to select the value from the list instead of using the index as value

Taifun

trev park

unread,
Feb 11, 2017, 12:50:01 PM2/11/17
to mitappinv...@googlegroups.com
index is the position in the list, value is what is contained at that position

I changed it to 'pick a random list item' list'get global lisy'list' instead of the random intergra from 1 to end of list 

and now it works

Reply all
Reply to author
Forward
0 new messages