Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Excluding previously selected values. (http://www.alice.org/community/showthread.php?t=4824)

 Jagatuba 08-01-2010 04:36 PM

Excluding previously selected values.

Hi All,

Although I am somewhat new to using Alice, I have caught on to most of the program's functionality. However, I am having trouble creating method to choose a random number.

I do know how to use variables and the random number function. However, I'm having trouble coming up with a logic that chooses a different random number each time the method is called.

It's tricky because here is what I want to do. I want to have an array called questionArray. This array will be populated with a number of questions that I want to ask the user at random. However, I do not want to repeat a question if it has already been asked.

It seems simple at first glance.

Create a checkArray.
Set randomNumber between 0 and size of questionArray.
Check randomNumber against all the values of the checkArray.
If the randomNumber is not equal to any of the numbers in checkArray,
Add it to the next index slot of checkArray then
Display item randomNumber in questionArray.
Else select a new randonNumber and perform the check again.

This was my initial instinct, but I found myself banging my head as it seems to require several nested loops, which get quite confusing quickly. I seem to be lost at how to solve this problem. Does anybody have any suggestions on how to solve construct this?

If I can just get a method constructed that returns a different random number every time, I think I can handle the rest of the logic involving question selection and display.

Cal

 Dameria 08-01-2010 05:41 PM

Can you create an integer list called "alreadyAsked" or something like that, and then just add the random number to that list once it is generated, and then the next time you generate a random number just say "if random number is one of the numbers in the list "alreadyAsked", choose a different random number. Else do nothing"? You would have to use that things on the bottom near "Wait" and "While" and "Loop", I think its called "For All Together", and you would assign the "alreadyAsked" list to that?

 zonedabone 08-05-2010 10:37 PM

Use a list instead. arrays are buggy. Whenever a question is asked, remove it from the list.

 All times are GMT -5. The time now is 03:35 PM.