Alice Community  

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

Reply
 
Thread Tools Display Modes
Arrays and Random Numbers
Old
lozirkid1245
Guest
 
Status:
Posts: n/a
Default Arrays and Random Numbers - 04-05-2008, 10:51 AM

Hello, I am somewhat new to Alice. I was wondering how I would set up an array for 2 people talking, each saying a statement in order.

I also need to make an array for 4 objects that will move down the road. Each object would be selected randomly and would move down the road as the 2 peoples conversation is going on.

I also need to write a world function that will return a random # of either 0 or 1, which will determine which speaker will leave in a certain direction after the conversation has ended.

Thanks again!!!
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-05-2008, 11:18 AM

Quote:
Originally Posted by lozirkid1245 View Post
Hello, I am somewhat new to Alice. I was wondering how I would set up an array for 2 people talking, each saying a statement in order.

I also need to make an array for 4 objects that will move down the road. Each object would be selected randomly and would move down the road as the 2 peoples conversation is going on.

I also need to write a world function that will return a random # of either 0 or 1, which will determine which speaker will leave in a certain direction after the conversation has ended.

Thanks again!!!
Good questions. You will find the answers to most of these and other similar questions in the free online tutorials at http://www.dickbaldwin.com/tocalice.htm

They were published to help students like you as you study and learn how to program.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm
   
Reply With Quote
Old
lozirkid1245
Guest
 
Status:
Posts: n/a
Default 04-06-2008, 11:17 AM

Thanks for the links, I got most of my problems figured out. However I still am unclear of one thing.

I need a World Function that will send a random # of either 1 or 0 to a world method which will decide which person will leave the screen first with a if/else loop.

I might have missed the section in the guide, but I would really appreciate it if you would point it out/show me how to do this. Thanks again.
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-06-2008, 11:24 AM

Because of a bug in the random number generator, trying to do an integer match is surprisingly hard. For a "coin flip" just set an output range (say 10) for the random number generator and then make the choice at the midpoint.
In pseudocode "If random number > 5 then coin=heads else coin = tails"
   
Reply With Quote
Old
lozirkid1245
Guest
 
Status:
Posts: n/a
Default 04-06-2008, 07:42 PM

Hmmm I am a little confused about the code.

All I really need is for a world function to either pick a 0 or a 1 (randomly), then send it to an IF/ELSE statement, it sounds simple enough but I cannot for the life find out how to do it. Sorry if I wasn't being completely clear.
   
Reply With Quote
Old
groucho
Guest
 
Status:
Posts: n/a
Default 04-07-2008, 01:28 AM

Quote:
Originally Posted by DrJim View Post
Because of a bug in the random number generator, trying to do an integer match is surprisingly hard.
DrJim, if you are referring to the "integer vs double" bug, mentioned in the bug list, that can be overcome by using a function like

Number public Number random0and1 ( ) {
return ( ( ( Random.nextDouble() minimum = 0 maximum = 1.99 integerOnly = true ) * 1 ) ) ; }

The random number generator seems to work fine otherwise, as I get consistent results on 0's and 1's using such a function.
Attached Files
File Type: a2w random0and1.a2w (172.8 KB, 141 views)
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-07-2008, 12:25 PM

I agree your approach works fine as long as the "integer" result from the random number is explicitly multiplied by a non-integer-type value. (Nothing else but an integer output should be possible - but it seems that ocassionally it is.)

The safest way is just to always multiply the output from the random number generator by 1.0 - making sure you explicitly include the decimal point, even though Alice may ignore it in the screen listing.
   
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 ©2021, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.