Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

How to prevent duplicate numbers

Mickey James
Registered User
Join date: 4 Nov 2006
Posts: 334
06-28-2007 20:22
I need a script that will generate a random number, do something based on the number generated, then generate another number in the same range, do something, etc.

I'm a long way there but what I can't figure out is how to ensure it only uses each number once. I tried doing each number generation/action cycle as a separate state, but the value of the first number defined in one state doesn't carry over.

So now I'm trying it all as one state, but I don't know how to set up a loop. What I need is something like:

Generate number B
If B==A (the previous number) go generate a new B.
Else (list of things to do depending what B is)
Kyrah Abattoir
cruelty delight
Join date: 4 Jun 2004
Posts: 2,786
06-28-2007 21:16
use the unix time, it never repeat.
_____________________

tired of XStreetSL? try those!
apez http://tinyurl.com/yfm9d5b
metalife http://tinyurl.com/yzm3yvw
metaverse exchange http://tinyurl.com/yzh7j4a
slapt http://tinyurl.com/yfqah9u
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
06-29-2007 01:24
Would this help?

list numbers = [0,1,2,3,4,5,6,7,8,9];

default
{
state_entry()
{
//llSay(0, "Hello, Avatar!";);
}
touch_start(integer total_number)
{
if(llGetListLength(numbers))
{
numbers = llListRandomize(numbers,1);
integer pick = llList2Integer(numbers,0);
numbers = llDeleteSubList(numbers,0,0);
llOwnerSay((string)pick + " chosen " + llList2CSV(numbers)+ " remain";);
}
else llOwnerSay("no numbers left";);
}
}
Russ Allen
scripter
Join date: 22 Mar 2007
Posts: 22
06-29-2007 01:38
I think this is what you need:

while(random_number_one == random_number_two)
{
random_number_two = llFrand(params);
}
//Now add here what you want to do next. Now the numbers will never be the same.

edit llFrand change params to the params you want...
Qie Niangao
Coin-operated
Join date: 24 May 2006
Posts: 7,138
06-29-2007 02:57
Kinda depends on the details of what's desired. If only "in a row" repeats are a problem, then a while loop as suggested by Russ is appropriate. If (as I read the original post), any repeat in the sequence is to be avoided, then the llListRandomize approach suggested by Very is appropriate--*if* the range of possible selections will be exhaustively or nearly exhaustively sampled. If, on the other hand, the selections will be a sparse sample of the range, then a kind of combination of these approaches would be better: each selection would be pushed on a list of already-selected values, and each selection would be in a while loop, reselecting until -1 != llListFindList(alreadySelected, [newSelection]);, or something like that.
Mickey James
Registered User
Join date: 4 Nov 2006
Posts: 334
06-29-2007 04:14
Thanks all... I'm not familiar with llListRandomize but I'll look it up. What I had used was a combination of llFrand and llRound to get a whole number 1-6. (That's just to test ... the finished product will have a much higher number.)

I do need to avoid any repeat at all. The script is to randomly choose objects to rez from a prim, and each object should be able to appear only once.
Mickey James
Registered User
Join date: 4 Nov 2006
Posts: 334
06-30-2007 18:15
From: Very Keynes
Would this help?

list numbers = [0,1,2,3,4,5,6,7,8,9];

default
{
state_entry()
{
//llSay(0, "Hello, Avatar!";);
}
touch_start(integer total_number)
{
if(llGetListLength(numbers))
{
numbers = llListRandomize(numbers,1);
integer pick = llList2Integer(numbers,0);
numbers = llDeleteSubList(numbers,0,0);
llOwnerSay((string)pick + " chosen " + llList2CSV(numbers)+ " remain";);
}
else llOwnerSay("no numbers left";);
}
}


This is on the right track but I still haven't completely cracked it. The problem is that I need the variable (where you used the word "pick" to have a different name each time through, because the object has to rez other objects at specified locations depending n the number chosen. However, it's not going to exhaust the list (for example, the list might have 15 possibiities but only six will be chosen.)

So what I tried was this, after defining the list globally and ging through the state entry part.

{
numbers = llListRandomize(numbers,1);
integer pick1 = llList2Integer(numbers,0);}

[a list of if-else statements telling it what to do if the number is 1, 2, 3, etc. ]

numbers = llDeleteSubList(numbers,0,0);

}

Then I essentially duplicated the block, but changing "pick1" to "pick2" and then to "pick3" and so on.

But that was not in fact removing the chosen numbers from the list, so in the llDeleteSubList arguments I changed the 0,0 to pick1,pick1 and so forth, and that still didn't work.

So am I misunderstanding how these functions should work? Note that I didn't use the llGetListLength line at all.
Qie Niangao
Coin-operated
Join date: 24 May 2006
Posts: 7,138
06-30-2007 19:04
May need to see the code at some point because the suggestion should work. To explain a bit what should be going on: The list of numbers is randomized once at the beginning, then start picking a number from the front of the list, and remove from the list that element (starting and ending at position 0) with "numbers = llDeleteSubList(numbers,0,0);" (hence it's "0,0"--the 0th element of the list--not "pick1, pick1";).

(It's perhaps too soon to complicate things, but rather than storing these selected random numbers in six separate variables, it may proove easier to store them in another, six element list, embedding in a for loop the pick, delete, and if/else statements, so the whole block of code doesn't have to be repeated six times in the script.)
Mickey James
Registered User
Join date: 4 Nov 2006
Posts: 334
06-30-2007 20:59
Ah. I was misunderstanding a couple of things then. I think I know how to fix it. Thanks!
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
07-01-2007 04:01
From: Mickey James
So am I misunderstanding how these functions should work? Note that I didn't use the llGetListLength line at all.


The llGetListLength is there as a fail safe to prevent it picking a number if there are no numbers left to pick. If you leave it out, and pick more than are availible, it will crash the script as the index will become negative.
Silje Russell
lsl geek
Join date: 2 Oct 2005
Posts: 63
07-01-2007 05:56
From: someone


default
{
touch_start(integer total_number)
{
string randomnr = (string)llGetUnixTime();
llOwnerSay(randomnr);
}
}







That way you never ever get same number
_____________________
Yes i know i got typos..
I got writing and reading problems.
but i am still a humen!!
Qie Niangao
Coin-operated
Join date: 24 May 2006
Posts: 7,138
07-01-2007 07:19
Ummm. A time value is certainly _unique_ for many years hence, but it's not _random_: Time is not only monotonic, but linear, and thus (almost) perfectly predictable. (Except, that is, in certain fiction genres. ;) )
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
07-02-2007 14:01
From: Qie Niangao
Ummm. A time value is certainly _unique_ for many years hence, but it's not _random_: Time is not only monotonic, but linear, and thus (almost) perfectly predictable. (Except, that is, in certain fiction genres. ;) )



And SL down times... oh wait you said fiction
_____________________
I'm back......