Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 20th, 2007, 2:53 PM   #1
scm007
Newbie
 
Join Date: Jan 2006
Posts: 27
Rep Power: 0 scm007 is on a distinguished road
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!
scm007 is offline   Reply With Quote
Old Feb 20th, 2007, 2:59 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 23rd, 2007, 2:48 PM   #3
scm007
Newbie
 
Join Date: Jan 2006
Posts: 27
Rep Power: 0 scm007 is on a distinguished road
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?
scm007 is offline   Reply With Quote
Old Feb 23rd, 2007, 3:45 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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]
Arevos is offline   Reply With Quote
Old Feb 23rd, 2007, 4:26 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Feb 24th, 2007, 3:51 AM   #6
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:11 AM.

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