View Full Version : Random Number Generator(school project due friday)
ok, i'm very short on time(have till friday), so plz respond asap.
how would i go about making a random number generator or if it can't be done, what could i use to replace it?
lanceA
05-31-2006, 09:28 AM
You may wish to recheck with your teacher and ask him/her if you can use Alice's built-in Random function. I'm surprised you were given an assignment to use this without it first being explained in class, or maybe part of your assignment was to investigate the functions included in Alice ?
The World object has a Random function.
Good luck.
well, the thing is, we weren't taught it. this is our teacher's first time using alice so originally, our assignment was to just mess around with alice for the rest of the year since we aleady took the ap exam. after doing that for a week, she decicded to make us make a 10 min. long animation since we were all basically doing that. she pretty much just taught us the basics nd told us to experiment which was fine with all of us
about the random function, totally missed that. does it work like the random generator method in java?
also, how do i get the time function to work?
lanceA
05-31-2006, 06:34 PM
Alice's Random function returns a 6 digit double. You give it a range of numbers, i.e., minimum to maximum. All of the numbers it returns will be >= to min but < max. i.e., 2.45712, 0.39718, etc. depending upon your max and min values.
For Alice's timer function you need to do several things: create a class-level variable (used to track number of seconds left) and then create a method in your timer object which accepts the variable as a parameter and then decrements it.
If, as you indicated, your teacher has sort of 'turned you loose' to explore Alice with minimal instruction you might want to 'explore' with a friend or as part of a group of your classmates - there's power in numbers ;)
i'm better than the rest of my class:p(but not by much, either way, we all try to help eachother)
for the random thing, would i be able to make it pick anumber from 1-10 or something like that
and for the time thing, is there a tut on that? cause i don't really know how to do anything other more advanced than to allow u to steer something and haave the camera and other things follow
edit: also, how can i use a variable in more than one method?
lanceA
06-01-2006, 07:09 AM
I normally wouldn't be doing this but since it's the end of the school year try this for your random number:
Creat a new world.
Place an If/Else statement in the world.myfirstmethod
Click the create a new variable button and name your variable randomNumber and select Number as its type. Click OK to close the Create New Local Variable window.
You should see your variable at the top of world.myfirstmethod.
Drag your randomNumber variable tile down to the If statement. A dialog box will appear asking you for a value. Select 1 (you will change this in the next step).
Select the world tile, click functions and scroll down to random number. Drag the random number tile to the set value tile and replace the number 1 with it.
Click more in the random number tile and set the minimum value to 1. Click more again and set your maximum number.
Place code in your If/Else construct to test your random numbers.
To test your program you will probably have to run it MANY times and you should (emphasis on the word "should") see close to a 50% split in the execution of your If/Else statement. If you simply test it one or 8 times you might see that the same code executes all the time.
Your earlier email mentioned that you just completed the AP CompSci Exams and therefore I assume you are aware that Java's random number generator leaves a great deal to be desired. I won't go into the details but if you studied AP Computer Science then you probably also know that the MBS (Marine Biology Simulation) program for that course uses a Singleton method for it's RandNumGenerator. This produces a "better" random number generator.
The only tutorial I know for the timer function is in the Alice textbook.
Good luck.
thx, i appreciate it, i'll test tommorrow, didn't have time today
also, where would find the alice text book, and is there an online version
lanceA
06-02-2006, 05:20 AM
You can purchase the book from either the publisher, Prentice Hall, or Amazon ($45.00). Sometime in June Prentice Hall will be releasing a "Brief-Edition" of the textbook (approximately $26.00).
The book is not supposed to be accessable for free on the internet, however you can subscribe to www.safarix.com and have access to the content online. I've never used them so I can not comment on the service.
ok, thx, i think i figured out the probability thing, but whenever i run the program it crashes
i'm not sure if u do this, but i've uploaded the file so u can help me figure it out
lanceA
06-02-2006, 11:38 AM
I don't debug other people's programs but what you might want to do when you have an error in your program is begin by eliminating some of your code and see if you can isolate where the problem might be.
For example:
If you disable all "play sound" calls in your program and then run it you will find that your program does not crash.
If you then add the calls for sound back into your program one at a time and test each time you can possibly determine that the sounds in your 3 functions are not causing the program to crash.
That only leaves the sound effect being called in your world.myfirstmethod, SpaceDock play sound.
Good luck
ok, thank you for all ur help, but i've tried everything, and still crashes the window. fortunantly, i've discovered that it only crashes when attempting to steer it with the directional arrow keys
Jabberwocky
06-05-2006, 06:46 PM
You have two problems that are making your world crash. One, like LanceA has said, is that the Air Raid Siren sound has a corrupt header. It's attached to the world object and the spaceship object. Delete it from both places and recreate it.
The other problem, like you've said, is the click events for the arrow keys. Change the values of the click events to some value other than "code revolutions per second"; try "0.25 revolutions per second".
The world looks neat. I'd like to see it when your done.
Did you ever get your video flicker problem fixed?
no, fortunantly, i get enough time at school to find another problem :p
btw, i present it on friday, also, if i set it to .25 rps, it'll be all screwy, i'm trying to make it like the flight simulator example world so if u can tell me how i get those values for the arrow key events, plz tell me:D
oh, one more thing, where is the air raid siren attatched to the spaceship object?
Jabberwocky
06-06-2006, 12:34 PM
I did notice once that after changing the speed value the spaceship would just spin instead of joining formation with the fighters. I found that if you'll just delete the key event for the up arrow key & recreate it will work just fine.
In the flight simulator example world, the arrow-key click events are setting the speed to "turnSpeed revolutions per second". Looking at the world details you'll find a variable called turnSpeed and it's set to 0.12. There is no difference in doing this than physically setting the speed to a value. The variable is created for convenience; you can reference it throughout the program, and if you ever need or want to change the turn rate later you can do so by just changing it in one place.
To mimic this go to World Details, then the Properties tab and click "create new variable". Create a variable called "code" (since you are already calling it this in your click events) and set it to 0.12.
As for the Air Raid Siren.... to find sound attachments look in the Details Windows & the Properties tab. Scroll down until you see Sounds. It's attached to both the World & the Spaceship (but only being called by the spaceship).
haha! it works, thank you very much. now there's just one more problem, why is the infinite loop, well, not infinitely looping:p
Jabberwocky
06-07-2006, 04:58 PM
Short answer:
In your Spaceship's Take Control method remove the Loop from around the Do Together. Then inside the Do Together wrap Loops around each individual command.
(The camera.follow_space_craft method already has a loop inside it and the roket1mala.lock_on_spacecraft method has an embedded While loop. So no need to have loops on those.)
Long Answer:
The Flight Simulator example world has good explanation for this in the comments of the World's Begin Flying method.
"A Do together is not finished until slowest animation in it is finished. This can create awkward pauses. The fix is to wrap an infinite loop around a Do together around each group of simultaneous animations that have the same duration."
It seems this is what you are trying to do in your Take Control method by wrapping an infinite Loop around a Do Together. However, your Do Together doesn't just contain a "group of simultaneous animations"; you also call other methods, each of which have numerous commands with varying durations.
Other Stuff:
1) Arrow-key Click Events: Recreate all of the arrow-key click events. The world kept intermittently crashing on me until I did.
2) Air Raid Siren: The Air Raid Siren is attached to the Spacedock not the Spaceship like I said in a previous post.
(To figure this stuff out on your own, notice how things are called. Look in the Details window of an object. Each object has properties, methods, and functions. Whenever you use one of these in your code the object name precedes the attribute name, method name, or function name. For example in your World's My First Method routine the call to play the air raid siren references Spacedock.air_raid_siren So you know to look in the Spacedock object's properties to find the sound attachment.)
3) Sound Duration: Looking at the durations of the sounds you are using I noticed, a lot of the sounds you have attached are extremely short, only a 10th of a second. Even if played with a longer duration in the code I still don't hear anything on most of them. Look at the Fighter's start_up2 sound for example. (Note: Scroll to the right when looking at a sound tile to see it's duration. Also, click on the green arrows next to the sound tile to test/play the sound.)
4) Hyperdrive Use Counter: You have code in your hyperdrive method to limit it's use. Currently though, the user can activate the hyperdrive as much as they want. Each time the hyperdrive method is called the counter is being reset because you are using a local variable which is initialized to zero.
You need to create a World class variable to keep track of how many times the hyperdrive is used. Variables created in the World class are global. That way you'll be able to retain the value of the counter over time.
So, create a world class variable. Then replace your references to the hyperdrive method's local variable (X) with the new World class global variable. Delete the local variable when you are done since you no longer need it.
thank you, there's just one final problem
when the rocket gets within 0 meters of the spaceship, the missile along with the spaceship is supposed to go to 0% opacity
also, how do i import regular 2D images?
Jabberwocky
06-08-2006, 09:47 PM
How do i import 2D images?
File --> Make Billboard (http://www.alice.org/community/showthread.php?p=355#post355)
Missile Strike
Yeah, your roket1mala.lock_on_space_craft method needs work. Write some pseudocode, get out pencil & paper and work out the logic of what you want to do in the simplest of terms, then go back to Alice and try to work it out.
For example, you say you want the spaceship and missile to go to zero opacity but you never set the ship's opacity. You do set the missile's opacity to zero if the missile gets within 0 meters of the ship, (it should kick out of the while loop and execute the set opacity command; remember though, the ship is still moving & the missile is coded with some funky herky-jerky random movement, so the missle actually touching the ship might never happen), but inside the while loop you contradict yourself and set the missile's opacity to 100% if the missle is less than 100 meters away. You need to rethink this method.
Good luck!
ok, well, i presented, but it didn't exactly go as planned
anyways, forgot who said they wanted it but, here ya go:
Jabberwocky
06-10-2006, 03:33 PM
It's probably a moot point now, but if you set the While loop, in your roket1mala.lock_on_space_craft method, to 5 meters or greater instead of zero the ship & rocket will become invisible.
Chrono
11-28-2006, 08:59 PM
change the volume to .25