Valdemar Virgo
Registered User
Join date: 23 Jul 2004
Posts: 11
|
07-30-2004 23:11
I'm stuck.
I have a variable: MaxNumber
I want to randomize MaxNumber the amount of times equal to MaxNumber, with no duplicates.
Does that make sense?
Example, if MaxNumber = 5, then I want to generate 5 random numbers, but never have any of the numbers repeat... like 4,5,2,1,3 would be okay, but 5,3,4,3,1 would not.
I was thinking about dumping it all into a list, MaxNumber will sometimes be as high as 400, 450, and I think sorting through a list of that size would be time-consuming.
Any suggestions, tips, pointers?
Thx, VV
|
Valdemar Virgo
Registered User
Join date: 23 Jul 2004
Posts: 11
|
07-30-2004 23:18
I should probably add that the number is generated one at a time, through a touch_event... That's why I'm thinking I have to do it as a list; each random number it generates be added to the list, and then that list scanned each time to check for duplicates.
The problem with that is once the odds are against it choosing a number not already in the list, it will take longer and longer and longer... ESPECIALLY when it gets up into the hundreds.
Should I generate the list ahead of time (by creating a list with ALL the possible numbers in it), then randomize it, then use that preset order (determined at the rez or reset of the object) to set the order the numbers are used?
??
|
Valdemar Virgo
Registered User
Join date: 23 Jul 2004
Posts: 11
|
07-30-2004 23:40
Here's the route I'm going...
I decided it's (probably) best to have an event when the script first starts to create a list with all the possible numbers in it...
My lists are:
list numbers = []; list temp = []; list final = []; list randomized = [];
other important variables:
integer intMaxNumber = 7;
I then do this for loop which works perfect in saying out loud each number, from 1 to 7:
for (x = 1; x <= intMaxNumber; x++) { integer newNumber = x; // llSay(0, (string)newNumber); }
The part commented works fine.
What I want to do now is create a list that is continuously added onto during this loop, so when the loop is done, a final list exists that has all the numbers in it, 1 to 7 (in this case).
I thought I could figure out how to do this, but my final list ends up just having the "7" in it when I llList2CSV it.
|
Siobhan Taylor
Nemesis
Join date: 13 Aug 2003
Posts: 5,476
|
07-31-2004 01:42
use llFrand to randomise...
to add to the list (bearing in mind I'm still on my first coffee of the morning)
final += newNumber;
_____________________
http://siobhantaylor.wordpress.com/
|
Valdemar Virgo
Registered User
Join date: 23 Jul 2004
Posts: 11
|
07-31-2004 02:02
Wouldn't llFrand, when looped like that comes up with duplicates. At least it did in my first versions of the script where I tried llFrand.
I just need to figure out how to keep adding to a list while in a "for" loop.
|
Catherine Omega
Geometry Ninja
Join date: 10 Jan 2003
Posts: 2,053
|
07-31-2004 02:16
llListRandomize is what you want for randomizing the list. To add to the list, just do 'list_name = list_name + variable_name;' and that'll add your variable to the list. Read the rest of the stuff on lists in the wiki. It's good for you. 
|
Siobhan Taylor
Nemesis
Join date: 13 Aug 2003
Posts: 5,476
|
07-31-2004 02:26
From: someone Originally posted by Valdemar Virgo Wouldn't llFrand, when looped like that comes up with duplicates. At least it did in my first versions of the script where I tried llFrand. Yes, but you can use llListFindList to compare... as in ... if (llListFindList(final, [newNumber]) = -1) { final += newNumber; llSay(0, (string)newNumber + " added to list"); }
From: someone I just need to figure out how to keep adding to a list while in a "for" loop.
ah integer i; for (i=0, i<=10, i++) { final += i; }
As Catherine said though... it's all in the Wiki Hope this all helps... edited for fonts and sizes etc.
_____________________
http://siobhantaylor.wordpress.com/
|
Grim Lupis
Dark Wolf
Join date: 11 Jul 2003
Posts: 762
|
07-31-2004 06:31
How many numbers are we talking about here? All integers? integer NUM_COUNT = 100;
list nums = []; integer i;
for (i = 0;i < NUM_COUNT;i++) { nums += ; }
nums = llListRandomize(nums, 1);
for (i = 0;i < llGetListLength(nums);i++) { llWhisper(0, (string)llList2Integer(nums, i)); }
The theoretical max of NUM_COUNT is 512. The realistic max varies drastically, depending on the size of the program. With a very (I mean VERY) small program (like, 5 lines), I could only get a little over 400 ints in the list before I acheived collision.
_____________________
Grim
"God only made a few perfect heads, the rest of them he put hair on." -- Unknown
|