![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 27
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Posts: 27
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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] |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
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]);
}
__________________
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ADA Random Seed of an Array | scm007 | Other Programming Languages | 1 | Mar 25th, 2007 9:05 PM |
| relation between array and pointer | n00b | C++ | 6 | Oct 12th, 2006 3:38 PM |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 10:50 AM |
| random numbers in 2D array | cwl157 | Java | 4 | Apr 29th, 2005 6:08 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |