Alice Community Arrays and Random Numbers
 User Name Remember Me? Password

 Thread Tools Display Modes
 Arrays and Random Numbers
 lozirkid1245 Guest   Status: Posts: n/a 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!!!
DickBaldwin
Guest

Status:
Posts: n/a
04-05-2008, 11:18 AM

Quote:
 Originally Posted by lozirkid1245 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

 lozirkid1245 Guest   Status: Posts: n/a 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.
 DrJim Guest   Status: Posts: n/a 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"
 lozirkid1245 Guest   Status: Posts: n/a 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.
groucho
Guest

Status:
Posts: n/a
04-07-2008, 01:28 AM

Quote:
 Originally Posted by DrJim 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
 random0and1.a2w (172.8 KB, 141 views)

 DrJim Guest   Status: Posts: n/a 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.

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     Community News and Announcements Alice 3     How Do I?     Works In Progress     Share Your World     Share Custom Classes     Bugs and Trouble Shooting     Suggestion Box Alice 2     How do I...?     Works-In-Progress     Share Worlds     Share Objects     Bugs and Troubleshooting     Suggestion Box Educators     Teaching with Alice     Teaching Using Alice 3     Teaching Using Alice 2     CS Principles and Alice     AP CSA and Intro to Java and Alice     Workshops General Discussion     Questions and Comments     The Lounge