Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Random number question

Klandon Melville
Registered User
Join date: 12 Nov 2005
Posts: 14
12-13-2005 10:57
Hello,

I'm relatively new to SL scripting and object oriented programming (so I've just showed my age).

What I want to do is generate three random numbers between X and Y, with no duplicates.

Generating the random numbers using the Frand function is no problem.

What I'm having a devil of a time figuring out is how to eliminate the duplicates. Normally I'd use a Jump command, however Jump only appears to move forward, not backward.

I'm sure there's some terribly elegant way to check to see if variables a, b, and c are all different, I just haven't figured it out.

Thanks for your time while SL is down.
Lit Noir
Arrant Knave
Join date: 3 Jan 2004
Posts: 260
12-13-2005 12:28
Hmmm 2 ways that I can think of off the bat.

The first, and likely faster and less memory intensive route:
CODE

float fNumber;
list lList = [ ];
integer gList;

//repeat this code for however many numbers you need, say with a for loop

while (gList != -1)
{
fNumber = [the randomize function];
gList = llListFindList(lList, (list)fRand);
}
lList = lList + fNumber;


This code has not been debugged, and I likely messed up the while statement, but this should give you the the idea.

The other method which is likely much worse, but very simple. Use a for loop to generate a list of suitable numbers. Use list randomize. Then just for loop back through the list for however many numbers you need.

EDIT TO ADD:
The first method makes the most sense for large ranges of possible numbers but you only need a few, think coordinates. The second method is probably better if you have a smaller range of numbers and you will use the majority of them, like a deck of cards as Strife said. And if I were you, I'd use Strife's code, even my "Hello World" scripts rarely compile right the first time.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
12-13-2005 12:30
jump works in both directions, the compiler doesn't work properly. For every lable only the first jump will lead to it. This is a major bug. Due to jumps fall from favor in programing best practices, they haven't fixed it (it's a known bug). If you want multiple jumps to lead to the same location, create multiple lables at that location.

That said.
CODE

list randIntList(integer count, integer x, integer y)
{
integer count;
list numbers;
integer temp;
y -= x;
while(count--)
{
do
temp = x + (integer)llFrand(y);
while(1 + llListFindList(numbers,[temp]))
numbers += temp;
}
return numbers;
}


If you were shuffling a deck of cards it would be better to use a llListRandomize

EDIT: grrr not fast enough
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
JackBurton Faulkland
PorkChop Express
Join date: 3 Sep 2005
Posts: 478
12-13-2005 12:38
how bout something along these lines

integer x;
integer y;
integer z;
integer done = 0;
x = randomNumber; //generate your random number

while(done != 2)
{
integer temp;
temp = randomNumber; //generate random number

if((temp != x) && (done == 0))
{
y = temp;
done++;
}
else if((temp != y) && (done == 1))
{
z = temp;
done++;
}
}
_____________________
You know what Jack Burton always says... what the hell?
JackBurton Faulkland
PorkChop Express
Join date: 3 Sep 2005
Posts: 478
12-13-2005 12:50
I edited my prior post to work for three random vaiables. lol i dont reads so wells
_____________________
You know what Jack Burton always says... what the hell?
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
12-13-2005 12:55
This could generate the same number for x and z.

From: JackBurton Faulkland
how bout something along these lines

CODE

integer x;
integer y;
integer z;
integer done = 0;
x = randomNumber; //generate your random number

while(done != 2)
{
integer temp;
temp = randomNumber; //generate random number

if((temp != x) && (done == 0))
{
y = temp;
done++;
}
else if((temp != y) && (done == 1))
{
z = temp;
done++;
}
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
JackBurton Faulkland
PorkChop Express
Join date: 3 Sep 2005
Posts: 478
12-13-2005 12:59
From: Strife Onizuka
This could generate the same number for x and z.


my bad lol
try this

integer x;
integer y;
integer z;
integer done = 0;
x = randomNumber; //generate your random number

while(done != 2)
{
integer temp;
temp = randomNumber; //generate random number

if((temp != x) && (done == 0))
{
y = temp;
done++;
}
else if((temp != y) &&;(temp != x) && (done == 1))
{
z = temp;
done++;
}
}
_____________________
You know what Jack Burton always says... what the hell?
Klandon Melville
Registered User
Join date: 12 Nov 2005
Posts: 14
Randomize function
12-13-2005 13:24
Thanks for all the responses!

After somewhat careful consideration, I believe the llListRandomize function may suit me best, I need three numbers from a set of 24 #'s, so it's easy to key in.


However, I putin this

list dick = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
list jane = llListRandomize(dick,1);

and I get a "syntax error" on the second line...

and it's pretty much a straight copy out of the Wiki....
Hot Tempura
Registered User
Join date: 2 Dec 2005
Posts: 3
12-15-2005 08:36
From: Klandon Melville
Thanks for all the responses!
list dick = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
list jane = llListRandomize(dick,1);

and I get a "syntax error" on the second line...

What code do you have surrounding it?