Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Excluding previously selected values.
Old
Jagatuba
Guest
 
Status:
Posts: n/a
Default Excluding previously selected values. - 08-01-2010, 04:36 PM

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.

Thanks in advance,
Cal
   
Reply With Quote
Old
Dameria
Senior Member
 
Dameria's Avatar
 
Status: Offline
Posts: 976
Join Date: Sep 2009
Location: United States
Default 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?


Check out my best projects so far and please comment:

Tic-Tac-Toe 2.0 ................... http://www.alice.org/community/showthread.php?t=3548
Alice Paint ........................... http://www.alice.org/community/showthread.php?p=23408
Real-Time Clock .................. http://www.alice.org/community/showthread.php?p=12725
Maze Game ......................... http://www.alice.org/community/showthread.php?t=4301
   
Reply With Quote
Old
zonedabone
Senior Member
 
zonedabone's Avatar
 
Status: Offline
Posts: 577
Join Date: Nov 2008
Location: In the interwebs
Default 08-05-2010, 10:37 PM

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


'Apple Macintosh' - An anagram of 'Complaints Heap'

M.A.C.I.N.T.O.S.H. - Machine Always Crashes, If Not, The Operating System Hangs.

You're *such* a mac person.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.