Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Software Design and Algorithms (http://www.programmingforums.org/forum64.html)
-   -   Random Seed of an array-- pseudocode? (http://www.programmingforums.org/showthread.php?t=12630)

scm007 Feb 20th, 2007 3:53 PM

Random Seed of an array-- pseudocode?
 
Here is the deal, I have an array, say array1 that I want to take a random seed of. How do I do this? Basically I want to take a random sampling of array1 and make a new array, called list that is built from these values, with no duplicates. A 3rd array would also be made, called outlist, which would be all the values in array1 that are not in list.

Thanks!

DaWei Feb 20th, 2007 3:59 PM

Isn't this a cross-post? Tsk, tsk. Please read the forum's rules.

Your terminology is not correct. A seed is a value that you use when initializing a random-number generator. If you want to select an array element randomly, generate a random number in the range of the number of elements that the array contains. If you don't want duplicates, you'll have to check for that. If you want to divvy up the contents, you'll have to write that. To avoid duplicates, you could also make a sequential list of numbers from 0 to the length of the array (there would then be no duplicates) and shuffle (randomize) it, then use it for the selection. This would prevent duplicate elements but NOT duplicate values. You'll just have to check for that.

scm007 Feb 23rd, 2007 3:48 PM

I purposefully avoided cross posting because the other post was looking for ADA code, but since it received no responses I thought I would go a step up.

Thanks for the tip! But... how would I shuffle a range of numbers randomly? Where can I find a shuffle algorithm?

Arevos Feb 23rd, 2007 4:45 PM

There was a post on this a while back, IIRC. One of the easiest shuffle algorithms is:
:

for i = 0 to (array.length - 1)
    r = random number from 0 to array.length - 1
    swap values of array[i] and array[r]


titaniumdecoy Feb 23rd, 2007 5:26 PM

If you want to avoid duplicate values, copy each unique value from the original list into a new list, say outlist. Then shuffle outlist and remove each element you add to list.

kruptof Feb 24th, 2007 4:51 AM

or you could shuffle the array by using some sort of key then swapping the values

:

        for(int i=0;i<15;i++)
        {
                j = arr[i]+key.at(j % strlen(key.c_str()));
                j = j % 15;
                swap(arr[i],arr[j]);
        }



All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC