Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# pick random nodes from xml doc

Posted on 2010-11-20
Medium Priority
459 Views
I have a page with 3 spaces for information pulling from an xml file. My xml file has 20 nodes of different  information.
What can I put on my page in javascript or asp to pull a random different 3 each time the page loads?
0
Question by:rivkamak
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 1

Expert Comment

ID: 34182169
You can write an algorithm using Random number implementation to give you three unique random numbers between 1 and 20 everytime you call that function.

Then you can pass those numbers to your processor which is processing XML node and it can give you the XML nodes data corresponding to those three numbers.
0

LVL 20

Accepted Solution

Proculopsis earned 2000 total points
ID: 34182171

Without knowing your xml structure, the following picks three values from an array:

``````<script>
// http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26629425.html

var nodes = ["one","two","three","four","five"
,"six","seven","eight","nine","ten"
,"eleven","twelve","thirteen","fourteen","fifteen"
,"sixteen","seventeen","eighteen","nineteen","twenty"];
var pick = Array();

while ( pick.length < 3 )
{
var index = randomTo( 20 );
while ( isIn( pick, index ) )
{
index = randomTo( 20 );
}
pick[pick.length] = nodes[index];
}

function randomTo( modulo )
{
return Math.floor( Math.random() * new Date().getMilliseconds() ) % modulo;
}

function isIn( array, value )
{
result = false;
for ( var i = 0; i < array.length; i++ )
{
if ( array[i] == value )
{
result = true;
break;
}
}
return result;
}
</script>
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
###### Suggested Courses
Course of the Month4 days, 8 hours left to enroll