View Full Version : Arrays and Random Numbers
lozirkid1245
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
04-05-2008, 11:18 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!!!
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
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
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
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
04-07-2008, 01:28 AM
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.
DrJim
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.