473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

randomize array

I'd like to randomly sort an array. A good method?
Jul 23 '05 #1
21 4166
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?


Untested (but modified from a working script):

function Random( low, high )
{
with( Math )
{
return floor(random() * ( 1 + high - low ) + low );
}
}

var unSorted = new Array( 5 );
var Sorted = new Array( 5 );
var used = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

for( ii=1; ii<=5; ii++ )
{
Used[ ii ] = false;
}

for( ii=1; ii<=5; ii++ )
{
randomNumber = Random( 1, 5 );
while( used[ randomNumber ] )
{
randomNumber = Random( 1, 5 );
}
sorted[ ii ] = inSorted[ randomNumber ];
used[ randomNumber ] = true;
}

Jul 23 '05 #2
Nik Coughin wrote:
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?
Untested (but modified from a working script):

function Random( low, high )
{
with( Math )
{
return floor(random() * ( 1 + high - low ) + low );
}
}

var unSorted = new Array( 5 );
var Sorted = new Array( 5 );
var used = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

for( ii=1; ii<=5; ii++ )
{
Used[ ii ] = false;
}

for( ii=1; ii<=5; ii++ )
{
randomNumber = Random( 1, 5 );
while( used[ randomNumber ] )
{
randomNumber = Random( 1, 5 );
}
sorted[ ii ] = inSorted[ randomNumber ];


Above line should read:

sorted[ ii ] = unSorted[ randomNumber ];
used[ randomNumber ] = true;
}


Jul 23 '05 #3
Nik Coughin wrote:
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?
Untested (but modified from a working script):

function Random( low, high )
{
with( Math )
{
return floor(random() * ( 1 + high - low ) + low );
}
}

var unSorted = new Array( 5 );
var Sorted = new Array( 5 );
var used = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

for( ii=1; ii<=5; ii++ )
{
Used[ ii ] = false;


Sorry, another typo. Should be:

used[ ii ] = false;
}

for( ii=1; ii<=5; ii++ )
{
randomNumber = Random( 1, 5 );
while( used[ randomNumber ] )
{
randomNumber = Random( 1, 5 );
}
sorted[ ii ] = inSorted[ randomNumber ];
used[ randomNumber ] = true;
}


Jul 23 '05 #4
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?


Ignore my previous posts. Didn't test it and it didn't work. Think it had
too many capitalisation related errors. This works:

<html>
<head>
<title>Test</title>
</head>
<body>
<script language="JavaS cript" type="text/javascript">
function Random( low, high )
{
with( Math )
{
return floor(random() * ( 1 + high - low ) + low );
}
}

var unSorted = new Array( 5 );
var Sorted = new Array( 5 );
var used = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

for( ii=1; ii<=5; ii++ )
{
used[ ii ] = false;
}

for( ii=1; ii<=5; ii++ )
{
randomNumber = Random( 1, 5 );
while( used[ randomNumber ] )
{
randomNumber = Random( 1, 5 );
}
Sorted[ ii ] = unSorted[ randomNumber ];
used[ randomNumber ] = true;
}
</script>
</body>
</html>
Jul 23 '05 #5
> Jeff Thies wrote:
I'd like to randomly sort an array. A good method?
Ignore my previous posts. Didn't test it and it didn't work. Think it

had too many capitalisation related errors. This works:
Thanks Nik. It's better than what I might have thought up!

Curious about the length initializations : var used = new Array( 5 ); Does
that save resources, or is there another reason?

Cheers,
Jeff

<html>
<head>
<title>Test</title>
</head>
<body>
<script language="JavaS cript" type="text/javascript">
function Random( low, high )
{
with( Math )
{
return floor(random() * ( 1 + high - low ) + low );
}
}

var unSorted = new Array( 5 );
var Sorted = new Array( 5 );
var used = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

for( ii=1; ii<=5; ii++ )
{
used[ ii ] = false;
}

for( ii=1; ii<=5; ii++ )
{
randomNumber = Random( 1, 5 );
while( used[ randomNumber ] )
{
randomNumber = Random( 1, 5 );
}
Sorted[ ii ] = unSorted[ randomNumber ];
used[ randomNumber ] = true;
}
</script>
</body>
</html>

Jul 23 '05 #6
Jeff Thies wrote:
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?


Ignore my previous posts. Didn't test it and it didn't work. Think
it had too many capitalisation related errors. This works:


Thanks Nik. It's better than what I might have thought up!

Curious about the length initializations : var used = new Array( 5 );
Does that save resources, or is there another reason?


To be honest, I don't know if it saves resources or even if it's necessary.
I don't know all that much about JavaScript, just the basic syntax. I'm
just lucky that it's similar enough to other languages that I've used that
pretty much everything I write works first time (except when I type
something out quickly and sloppily and fuck up the capitalisation) . I've
written in a lot of different languages (BASIC, Pascal, COBOL [yuck], c/c++,
vb, DarkBASIC, delphi, JavaScript, *nix shell, etc. etc. etc.), and I've
gotten into the habit of declaring arrays before I use them.
Jul 23 '05 #7
Nik Coughin wrote:
Jeff Thies wrote:
Jeff Thies wrote:
I'd like to randomly sort an array. A good method?

Ignore my previous posts. Didn't test it and it didn't work. Think
it had too many capitalisation related errors. This works:


Thanks Nik. It's better than what I might have thought up!

Curious about the length initializations : var used = new Array( 5 );
Does that save resources, or is there another reason?


To be honest, I don't know if it saves resources or even if it's
necessary. I don't know all that much about JavaScript, just the
basic syntax. I'm just lucky that it's similar enough to other
languages that I've used that pretty much everything I write works
first time (except when I type something out quickly and sloppily and
fuck up the capitalisation) . I've written in a lot of different
languages (BASIC, Pascal, COBOL [yuck], c/c++, vb, DarkBASIC, delphi,
JavaScript, *nix shell, etc. etc. etc.), and I've gotten into the
habit of declaring arrays before I use them.


That said, looking through the Wrox JavaScript Programmer's Reference, it
seems that they either need to be or should be declared.
Jul 23 '05 #8
Nik Coughin wrote:
Jeff Thies wrote:
I'd like to randomly sort an array.


I would prefer randomizing by record swapping:
<script type="text/javascript">

var unSorted = new Array( 5 );
var randomlist = new Array( 5 );

unSorted[ 1 ] = '<a href="one.html" >One</a>';
unSorted[ 2 ] = '<a href="two.html" >Two</a>';
unSorted[ 3 ] = '<a href="three.htm l">Three</a>';
unSorted[ 4 ] = '<a href="four.html ">Four</a>';
unSorted[ 5 ] = '<a href="five.html ">Five</a>';

// random number generation
function Random( low, high ) {
return Math.floor(Math .random() * ( 1 + high - low ) +
low );
}

// fill a new array "randomNumb er" sequentially
for( ii=1; ii<=5; ii++ ) {
randomlist[ ii ] = ii;
}

// randomize the randomNumber array by record swapping
for( ii=1; ii<=5; ii++ ) {
var randomNumber = Random( 1, 5 );
var temp = randomlist[ ii ]
randomlist[ ii ] = randomlist[ randomNumber ]
randomlist[ randomNumber ] = temp
}

// indirect output the so randomized list
for( ii=1; ii<=5; ii++ ) {
document.write( unSorted[randomlist[ ii ]]+"<br>")

// or fill a new array "myNewArray ":
// myNewArray[ ii ] = unSorted[randomlist[ ii ]]
}

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #9
On Fri, 2 Apr 2004 14:12:22 +1200, Nik Coughin <nr***********@ woosh.co.nz>
wrote:
Nik Coughin wrote:


[snip]
To be honest, I don't know if it saves resources or even if it's
necessary. I don't know all that much about JavaScript, just the
basic syntax. I'm just lucky that it's similar enough to other
languages that I've used that pretty much everything I write works
first time (except when I type something out quickly and sloppily and
fuck up the capitalisation) . I've written in a lot of different
languages (BASIC, Pascal, COBOL [yuck], c/c++, vb, DarkBASIC, delphi,
JavaScript, *nix shell, etc. etc. etc.), and I've gotten into the
habit of declaring arrays before I use them.


That said, looking through the Wrox JavaScript Programmer's Reference, it
seems that they either need to be or should be declared.


You need to initialise the variable or property to be of an array type
using either of

a = [];
a = new Array();

but you needn't specify a size. Writing

a[ 7 ] = 'The only entry';

will resize the zero-length array to include eight elements, the last of
which contains a string. The first seven will remain undefined: a sparse
array.

There might be some speed improvements by specifying an adequate initial
size, but only if arrays in JavaScript are implemented as contiguous
memory blocks, which might not be the case (probably implementation
dependent).

The same is true for an object. You need to initialise the variable or
property to an object using either of[1]

o = {};
o = new Object();

before you assign any properties or methods.

Mike
[1] You could also use a function as a constructor.

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #10

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

Similar topics

8
1897
by: knoak | last post by:
Hi there, I have i site with photo-albums on it. to create a new album all i do is create a new folder. I use the following code. My only question is, how can i randomize the output, so the order of the folders is different every time? ++++++++++++++++++++++++++++++++++++++++++++++++++++
2
15015
by: Fieldmedic | last post by:
I'm trying to determine the best way to randomize a set of array items. I'm thinking that I could use an arraylist and have it use the lower and upper bounds as the limits. Any suggestions? Cheers, Steve
3
14631
by: Gaffer | last post by:
Hello Is there a way in which I can make certain parts of Html on my website random so that each viewer will see different material if they refresh the page or come back onto the website later? I am currently using the "<!--#include virtual="cgi-bin/menu1.txt" -->" command to display my menus on every page of my site, so that the same material is shown on every page, and I would like a way that I can randomize this perhaps. I got a...
1
3424
by: Ellen Manning | last post by:
I've got an A2K database with a report that generates any number of random medical record numbers. The user inputs how many numbers they want and report uses the Randomizer function found on "The Access Web" site to generate these numbers. Out of 38000 records, there are 900 unique medical record numbers. I run the randomizer function on a query of these unique medical record numbers. The user states there are too many duplications...
4
2222
by: Arnold | last post by:
Hi there, Here's the situation--there is a text field in a form in which students will key in data. On the keypress event, I'd like for different sounds to be played for each character typed, either randomly or in a loop (I have a number of small, cool, computer-like sounds). I can get one sound to play for the keypress event, but don't know how to play multiple sounds. Here's the codes for playing one sound...
1
3765
by: Badass Scotsman | last post by:
Hello, This code is supposed to generate a random string each run, however I have had it live on a few sites, and it seems to create repeat strings all over the place. ------------------------------------------------------------- <% FUNCTION GetRandomCode(randomcode,codelength,numberofcombinations) codecharacters = 35
1
1564
by: Samuel Shulman | last post by:
Any function to randomize strings? thank you, Samuel
6
2399
by: mrtaka79 | last post by:
Okay, first of all, I'm a complete noob, so go easy on me. I have this code that works perfectly for me. The only thing I want to add is to randomize the pictures/links that show up. Can anyone just tell me where to add additional code to the following? BTW this is the only code I could find that works in my Beta Blogger sidebar. <a href="javascript:gotoshow()"><img border="0" width="300" src="/OneMissedCall1.jpg" name="slide"...
5
2893
by: gggram2000 | last post by:
Hi, I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg. I randomize 1-100 and I get 50. I randomize 1-900 on another input field and I get 578. I randomize 2-56 and get 31. " " and get 43. " " and get 78. So I have five numbers: 50, 578, 31, 43 and 78.
0
8613
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.