|
Need a Random number script and Arrays
About Truespace Archives
These pages are a copy of the official truespace forums prior to their removal somewhere around 2011.
They are retained here for archive purposes only.
Need a Random number script and Arrays // Scriptorium
Post by notejam // Jan 24, 2008, 9:16pm
notejam
Total Posts: 191
|
Hi,
I need a random number script. It should be able to generate numbers between -12 to 12. That might be done by having a math line of something like r = random - random and random be the random number generated by what ever function can do it, and if it gives a positive number
Then I need an array with about 20 elements. I want to set the value of these elements, and then pick the contents by picking a index.
In other words, a(1)=6 a(2)=4 a(3)=7 and so forth, and then have a block that on input of 1, can pick a(random) and return the value to me, where random in this case would be in range of 1 to 3, but I really will use about 20.
Any ideas? |
Post by Délé // Jan 25, 2008, 12:06am
Délé
Total Posts: 1374
|
Hi notejam,
I'm not sure I understand exactly what you're wanting to do with the array, but the random number generation is easy enough. The code below should generate a random number between -12 and 12. First, in the attributes tab, create an output attribute called "result" and make it of type "number".
// output random value between -12 and 12
params.ConValue("result") = Math.random() * 24 - 12;
hth |
Post by notejam // Jan 25, 2008, 5:01am
notejam
Total Posts: 191
|
Ok will try and explain it better. I really should of said list instead of array.
I want to have 3 varibles a(1) a(2) and a(3)
I want the user of the script to be able to type in the values of those varibles.
Then I want a script that will pick a number from 1 to 3 and if its 1, it will have as output the value contained in a(1) but if its 2, it will output the value of a(2) instead, and if its 3, it will output the value of a(3)
So its just a simple pick list, where a random number picks what the value is going to be from the list, by use of the random number as the index into that list. So its output = a(r) where r is a number between 1 and 3 generated randomly, which outputs the value of list element a(r)
The purpose is to make a probability list that the user can setup, and then from then on, the program picks numbers from that list at random.
So if the user set all numbers to 3, then the list can only ouput the number 3, but if they set first varible to 2, and the other two to 3 then the list is set up to most likely return 3 as result, but at times return 2
So its in effect a user created, list that if one repeats any number in the list, they in effect, adjust the list so its going to more likely return that number than a less often used number in the list.
I will use a bigger list, but for this example, only have a 3 element list. After I see how to do it for that, then I can expand it to as big a list as I want to use, and I am uncertain at the moment how big that will be, but think it will be about 20 user set values. |
Post by Délé // Jan 26, 2008, 2:23am
Délé
Total Posts: 1374
|
I see now, that's easy enough. I figured it would be easier to show you then to explain so I whipped up a short video.
In the video I use the Math.random() function to first generate a random floating number between 0 and 1. Then I multiply that by 2 to make that random number increase to between 0 and 2. Last I use the Math.round() function on that result to round off that random float number to the nearest integer. So in the end it generates a random number, either 0, 1, or 2.
In the video I use an if/else statement to output the different user inputs depending on the random value, which works fine. A switch/case statement is usually more efficient in cases like this though. Below is the same code only with a switch/case instead of the if/else.
// Execute
// Called to execute the command
function Execute(params)
{
var input1 = params.ConValue('input1');
var input2 = params.ConValue('input2');
var input3 = params.ConValue('input3');
// Generate a random number between 0 and 2...then round it off
RandomNumber = Math.round(Math.random() * 2);
switch (RandomNumber)
{
case 0:
params.ConValue("result") = input1;
break;
case 1:
params.ConValue("result") = input2;
break;
default:
params.ConValue("result") = input3;
}
}
hth |
Post by notejam // Jan 27, 2008, 5:13am
notejam
Total Posts: 191
|
Thanks Dele, and extra thanks for making a video to explain it. That was quite helpfull. |
|