Computer Science Canada Generating random number between 1-10 and operator (*,+,-,/) |
Author: | aqazwsx1 [ Fri Feb 13, 2009 8:33 pm ] |
Post subject: | Generating random number between 1-10 and operator (*,+,-,/) |
Hi, how do I generate a random number between 1-10 and operator (*,+,-,/) ? Thanks! ![]() |
Author: | Insectoid [ Fri Feb 13, 2009 8:44 pm ] |
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) |
Rand.Int (1, 10) for the operators, assign each to a number between 1 and 4 and have a Rand.Int in an if statement to select one. |
Author: | aqazwsx1 [ Fri Feb 13, 2009 8:53 pm ] |
Post subject: | Re: Generating random number between 1-10 and operator (*,+,-,/) |
I don't really get the part about the operators |
Author: | Insectoid [ Fri Feb 13, 2009 9:47 pm ] |
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) |
Make a random number, and if it equals one, the operator is +, if it = 2, operator is -, etc. |
Author: | aqazwsx1 [ Fri Feb 13, 2009 11:35 pm ] | ||
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) | ||
Ok so I tried to make the program which generates simple +,-,*,/ calculations but It doesn't work properly. What do I need to fix? Thanks
|
Author: | Dark [ Tue Feb 17, 2009 7:18 pm ] |
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) |
For the operators... int num = (4* Math.random()+1); char operator; switch (num) { case 1: operator = '+'; break; case 2: operator = '-'; break; case 3: operator = '*'; break; case4: operator = '/'; break; } |
Author: | Insectoid [ Tue Feb 17, 2009 8:49 pm ] |
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) |
Just a quick semi-related question, is it possible to convert a string to an expression? |
Author: | A.J [ Tue Feb 17, 2009 8:50 pm ] |
Post subject: | Re: Generating random number between 1-10 and operator (*,+,-,/) |
just make an array of size 4 including the operators, then after you have made your random number 'x' from 1 - 4, then look it up in your array at 'x'. |
Author: | DemonWasp [ Tue Feb 17, 2009 9:21 pm ] |
Post subject: | RE:Generating random number between 1-10 and operator (*,+,-,/) |
@insectoid: In many languages, that's quite possible. For example, Javascript has eval(string) which will run the JS interpreter on anything stored in that string. The use of this in real applications is at best incredibly stupid, however, as it could lead to code-injection issues and will likely result in vastly slower performance than normal javascript. In pure Java, you can use the reflection API to execute named methods (I've done this, I made a cute little 150-line program that would execute whatever Java code was specified in a certain format). Again, not recommended. You can also, with Java, write code to a something.java file, invoke the compiler on it, then link it in. This strategy is frequently used in upper-end IDEs and webservers so that new code can be loaded on-the-fly, avoiding the need to stop the program / webserver while debugging (though this is generally NOT used to deploy code to a production server). |