What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Random numbers, prevent from being used more than once

Random numbers, prevent from being used more than once
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Random numbers, prevent from being used more than once
What is being requested can be done in essentially just 3 steps/lines of code!!

(if you don't count in 1 line for generating the initial random number and 1 line to return the new lyric, which each method would have...)

And if it wasn't for the 'undefined' problem, it would be just 2 steps/lines of code!!!!



----------------------------------------------------------------------



quote:
Originally posted by Volv
I think foaly's is the simplest working code.
Actualy foaly's method is not going to work, it is fatfreechicken's method which would be working.

The method used by fatfreechicken (although a bit overcomplicated than it needs to be) is actually a very widely used method to generate a random set of numbers in such a way that no number will be drawn twice (or trice, quar...blah, whatever. :D)...

foaly's method doesn't return anything if the randomly generated lyric already exists in the past 10 lines! His function would simply stop. And even if it was fixed to regenerate a new random number when the current one already exists instead of simply quiting (which I assume is what he wanted to do ;)), his method will be dead slow because in the worst case scenario his routine would take a very long time because it constantly draws a number/lyric which was already used before and thus it needs to regenerate a new one again. The bigger the ratio of uniqueness vs the number of lyrics is, the slower such a routine will become (eg: use his routine to generate lyrics out of 100 possible lyric lines where 99 continues lines can't have the same lyric... it would take a very long time).

roflmao456's method simply fills some random items of an array (not even all items for that matter).

markee's method is a variation on what I had done (though I didn't posted this yet because I was at work all day). But it is again overcomplicated and containes the big and slow 'regenerate until you have a valid number' kind of infinite loop like foaly....



----------------------------------------------------------------------



quote:
Originally posted by Volv
Although I think instead of:
count=count%9
You should replace it with:
count= (count==9 ? 0:count);

Because you don't want to have unnecessarily large integers being stored in memory.
it wouldn't make a difference.




----------------------------------------------------------------------



Here is my method:

(it might be possible that this is what fatfreechicken wanted to do in the first place though):

You have an array full of all the possible lyric lines (to choose from).
You have (currently an empty) array of used lyrics.

STEP 1:

Each time you draw a new random lyric line you remove that lyric line from your original array and add it to the array of used lyrics.

Now your original array wont contain that lyric line anymore.  And thus there is no need to check anything at all, you wont be able to choose it again anyways (= no unneeded and long loops like with foaly's method).

STEP 2:

Move the 10th element of the used lyrics array back to the original big array of lyrics (because you must be able to choose it again).

DONE!

Do this for as long as you want... The function would be instant, no loops at all! And it always makes sure you would have 10 different lines... thus no infinite loops, not even a slow 'check against all array elements' check...

useable directly out of the box:
code:
var arrLyrics = new Array();
var arrPrevious = new Array();
var nUniqueness = 10; // positive number, should always be lower than the total number of possible lyric lines!

function GetAllLyrics() {
        // normally you read in all the possible lyrics from a file here
        return Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
}

function GetRandomLyric() {
        // get a random lyric line from the 'possible lyrics' array
        var rndLyric = Math.floor(Math.random() * arrLyrics.length);
        // step 1: add new line to 'used lyrics' array and remove it from the 'possible lyrics' array
        arrPrevious[nUniqueness-1] = arrLyrics.splice(rndLyric, 1);
        // step 2: add last lyric line from the 'used lyrics' array back to the 'possible lyrics' array
        if (arrPrevious[0] !== undefined) arrLyrics.push(arrPrevious[0]);
        // step 3: remove last lyric line from the 'used lyrics' array
        arrPrevious.shift();
        // you're done: return the new lyric line
        return arrPrevious[nUniqueness-2]
}

// EXAMPLE START (get 20 lyrics)
        arrLyrics = GetAllLyrics();
        for (var i=0; i<20; i++) {
                Debug.Trace('New lyric: ' + GetRandomLyric());
        }
// EXAMPLE END


;)


EDIT: PS: stu, don't forget to use the var statement if you use/initialize a new variable inside a function. Not doing so will make that variable a global variable instead of a local variable.

This post was edited on 07-05-2007 at 12:53 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-05-2007 12:34 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Random numbers, prevent from being used more than once - by stu on 07-03-2007 at 10:14 PM
RE: Random numbers, prevent from being used more than once - by pollolibredegrasa on 07-03-2007 at 10:33 PM
RE: Random numbers, prevent from being used more than once - by foaly on 07-03-2007 at 10:34 PM
RE: Random numbers, prevent from being used more than once - by roflmao456 on 07-04-2007 at 03:15 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 06:01 AM
RE: Random numbers, prevent from being used more than once - by markee on 07-04-2007 at 02:17 PM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 02:31 PM
RE: Random numbers, prevent from being used more than once - by markee on 07-04-2007 at 02:35 PM
RE: Random numbers, prevent from being used more than once - by foaly on 07-04-2007 at 04:00 PM
RE: Random numbers, prevent from being used more than once - by stu on 07-04-2007 at 04:50 PM
RE: Random numbers, prevent from being used more than once - by roflmao456 on 07-04-2007 at 05:22 PM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 05:47 PM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 12:34 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 12:51 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 12:55 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 01:03 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 01:32 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 01:55 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 02:00 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 02:18 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 02:32 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-05-2007 at 02:45 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 02:51 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:07 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-05-2007 at 03:11 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:22 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:36 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:45 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:47 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:55 AM
RE: RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 04:23 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 04:30 AM
RE: Random numbers, prevent from being used more than once - by foaly on 07-05-2007 at 10:57 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 04:59 PM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 11:39 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On