473,569 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shuffle array elements (3 to end of array) in random order

I'd like to reorganize the third, fourth, fifth and sixth, as well as any
elements thereafter in an array in random order:

var a = new Array('first',' second','third' ,'fourth','fift h','sixth','etc ')

In other words, the first, second and third element should remain in
position 0, 1 and 2, while the fourth, fifth and sixth, etc. should appear
in random order.

Can anyone recommend a method to do this?

May 6 '07 #1
9 5328
Lee
Tuxedo said:
>
I'd like to reorganize the third, fourth, fifth and sixth, as well as any
elements thereafter in an array in random order:

var a = new Array('first',' second','third' ,'fourth','fift h','sixth','etc ')

In other words, the first, second and third element should remain in
position 0, 1 and 2, while the fourth, fifth and sixth, etc. should appear
in random order.

Can anyone recommend a method to do this?
First you need to describe what you want in a way that isn't
self-contradictory. Then you should read this newsgroup's FAQ,
particularly the note box in this section:
http://www.jibbering.com/faq/#FAQ4_22
--

May 6 '07 #2
Lee wrote:

[...]
First you need to describe what you want in a way that isn't
self-contradictory. Then you should read this newsgroup's FAQ,
particularly the note box in this section:
http://www.jibbering.com/faq/#FAQ4_22
Thank you for pointing me to that specific FAQ including those noteboxes
with links to some pretty complex math!! I hope the answer hides somewhere
in there....

In which way do you mean my description is self-contradictory?

To be more precise, after reorgnizing the latter part of the array, i.e.
starting from 3 index, the intended result, or the properties of the new
array, could be for example:

'first','second ','third','four th','fifth','si xth','etc'
or
'first','second ','third','fift h','fourth','si xth','etc'
or
'first','second ','third','sixt h','fifth','fou rth','etc')
or
'first','second ','third','etc' ,'fourth','sixt h','fifth'
etc.

The first 3 elements should remain in the position of the original array,
while the remaining elements should be simply reshuffled in a random order,
appearinging only once in any one position.

What is best here? Would it be to create an array without the first 3
elements and then add them into a randomly generated array of specified
elements?
May 6 '07 #3
Tuxedo said the following on 5/6/2007 10:46 AM:
Lee wrote:

[...]
>First you need to describe what you want in a way that isn't
self-contradictory. Then you should read this newsgroup's FAQ,
particularly the note box in this section:
http://www.jibbering.com/faq/#FAQ4_22

Thank you for pointing me to that specific FAQ including those noteboxes
with links to some pretty complex math!! I hope the answer hides somewhere
in there....

In which way do you mean my description is self-contradictory?
"I want my array in random order but I want to control what is or is not
random" is self-contradictory.

<snip>
The first 3 elements should remain in the position of the original array,
while the remaining elements should be simply reshuffled in a random order,
appearinging only once in any one position.

What is best here? Would it be to create an array without the first 3
elements and then add them into a randomly generated array of specified
elements?
Yes.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 6 '07 #4
Randy Webb wrote:

[...]
"I want my array in random order but I want to control what is or is not
random" is self-contradictory.
That was not exactly what I meant, but what I did mean was for some but not
all elements of an array to be reorganized in random order. But I think you
understood by now that I was not referring to the entire array as such.
Yes.
Thanks for the tip, which by the sound of it, I guess must be easier than
randomly reorganise only a specified segment of the complete array.

My question is therefore reduced and simplified to: How can I randomly
reorganise the elements of the following array?

var a = new Array('fourth', 'fifth','sixth' ,'etc')

May 6 '07 #5
Tuxedo wrote:
Thanks for the tip, which by the sound of it, I guess must be easier than
randomly reorganise only a specified segment of the complete array.
To answer myself, with some extra help from about.com
(http://javascript.about.com/library/blsort2.htm) the following solution
appears to work just fine:

function randOrd(){
return (Math.round(Mat h.random())-0.5);
}

var a = new Array('fourth', 'fifth','sixth' ,'etc')
a.sort(randOrd)
a.unshift('firs t','second','th ird')
May 6 '07 #6
Lee
Tuxedo said:
>
Lee wrote:

[...]
>First you need to describe what you want in a way that isn't
self-contradictory. Then you should read this newsgroup's FAQ,
particularly the note box in this section:
http://www.jibbering.com/faq/#FAQ4_22

Thank you for pointing me to that specific FAQ including those noteboxes
with links to some pretty complex math!! I hope the answer hides somewhere
in there....

In which way do you mean my description is self-contradictory?
You said:

I'd like to reorganize the third, fourth, ...

In other words, the first, second and third element should remain ...

The "third" element appears both in the list to be reorganized and
in the list to remain in place.

Look through those links for references to shuffling. You'll find
code that you can copy and paste. Once you're familiar with it,
you can decide whether to modify it slightly to begin at the third
(or is it fourth?) position, or whether you want to split it before
you shuffle and rejoin afterwards.
--

May 6 '07 #7
Lee wrote:

[..]
In other words, the first, second and third element should remain ...
Yes. Maybe there was some confusion with regards to my definition of the
starting point.

[..]
Look through those links for references to shuffling. You'll find
code that you can copy and paste. Once you're familiar with it,
you can decide whether to modify it slightly to begin at the third
(or is it fourth?) position, or whether you want to split it before
you shuffle and rejoin afterwards.
Thank you for the tips!
May 6 '07 #8
In comp.lang.javas cript message <f1************ *@news.t-online.com>,
Sun, 6 May 2007 18:11:25, Tuxedo <tu****@mailina tor.netposted:
>Tuxedo wrote:
>Thanks for the tip, which by the sound of it, I guess must be easier than
randomly reorganise only a specified segment of the complete array.
Possibly easier; but not otherwise better.

>To answer myself, with some extra help from about.com
(http://javascript.about.com/library/blsort2.htm) the following solution
appears to work just fine:

function randOrd(){
return (Math.round(Mat h.random())-0.5);
}

var a = new Array('fourth', 'fifth','sixth' ,'etc')
a.sort(randOrd )
a.unshift('fir st','second','t hird')
That method was discussed here some years ago, as an example of amusing
but ludicrous programming.

The specification of Array.sort calls for repeatable comparisons; the
sort algorithm to be used is not defined, and may vary from browser to
browser, version to version. With non-repeatable comparison, the result
cannot be guaranteed : the sort algorithm might not even finish in some
systems.

An efficient shuffle of N elements takes time O(N), and uses O(N) calls
of Random. The sort algorithm will take time at least about O(NlogN)
calling Random O(2NlogN) times.
The first question you should consider is whether you actually need a
Shuffle. A Shuffle starts with all the elements present on some order
and ends with them all on an independent order, probably in the same
Array. You might be able to install the first three elements in direct
order, then the rest in random order.

For shuffle-in-place, it would be easier to keep the last three elements
unmoved, by doing a standard shuffle but lying about the length. So,
consider

function Random(X) {
return Math.floor(X*(M ath.random()%1) ) /* %1 : Opera */ }

function ShuffleX(Q, x) { var R, T, J
for (J=Q.length-1 -x ; J>0 ; J--)
{ R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
return Q }

A = [1,2,3,4,5,6,7,8 ,9]
A = A.reverse()
ShuffleX(A, 3)
A = A.reverse()

In that, ShuffleX is the FAQ-cited standard (Knuth?) algorithm, modified
by the addition of the parameter x.

Now you can optimise by rewriting ShuffleX so that it works the other
way round, and omit the reversals.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
May 6 '07 #9
Dr J R Stockton wrote:

[..]
function Random(X) {
return Math.floor(X*(M ath.random()%1) ) /* %1 : Opera */ }

function ShuffleX(Q, x) { var R, T, J
for (J=Q.length-1 -x ; J>0 ; J--)
{ R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
return Q }

A = [1,2,3,4,5,6,7,8 ,9]
A = A.reverse()
ShuffleX(A, 3)
A = A.reverse()

In that, ShuffleX is the FAQ-cited standard (Knuth?) algorithm, modified
by the addition of the parameter x.

Now you can optimise by rewriting ShuffleX so that it works the other
way round, and omit the reversals.
Not that I've seen the ludicrous about.com + unshift method fail on any
browser I've tested it on so far, but hey, your solution certainly sound
like the more sophisticated one!! Thanks for the detailed code example!

May 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3005
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file and counts hits based on interval - day, month or year. $avs = file($viscounter); foreach ($avs as $val) { if($val>(time()-(86400))) {
4
13225
by: Frank & Janny Plaza | last post by:
I am trying to write a program in which the users will each enter their names and when all names have been entered, I want to randomly sort this list of names several times and then show the order in which they have beeen sorted. I tried using sort(), but this only sorts the names in the order in which they are entered. Any suggestions? Thanks,...
15
3313
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI layer. EG. bits-layer 1. Any way, I want the quiz to reorder the problems each time I take it. Here is part of the code i did so far for 62 ...
22
17850
by: Nhmiller | last post by:
Is there a way to do this? Thanks. Neil Cat Paintings At Carol Wilson Gallery http://www.carolwilsongallery.com
8
5221
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if the property is ReadOnly then the expanded array elements can still be edited. I need to make the elements ReadOnly if the property is ReadOnly. Is...
24
4358
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an...
4
2271
by: kevincw01 | last post by:
Anyone have a clever way to retrieve for example, items 0-29 from an array of size N>29, in random order? The catch is that I dont want to print any items more than once and I dont want to miss any. This is for a tag cloud where I have sorted them by popularity but now I need to display only the top 30. I do this now but they are in order...
2
1076
by: Chiefy | last post by:
Hey, Im doing a project in C# and I need to be able to take an array (say StudentNames), randomise the contents, and then place them in the random order into a new array (randomStudentNames). Thankyou. x
2
2326
by: dniom | last post by:
Hi. Can you help me please? Is there any way to show a unique random item from an array, every time you refresh the page? In other words how do I show the items from an array in random order, one by one, by reloading the page? Moreover the page have to be refreshed by pressing the "submit" button... Thats what I achieved so far :) but it...
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.