简体   繁体   中英

shuffle two consecutive blocks of data

I have data file (php) with 24 blocks of data, structured as below.

//ts1
$P[] = [0];
$S[] = [[1,95.8172406762736],[2,104.97526726371],[3,112.03839938973],[4,101.70457396977],];
$F[] = [];
$FB[] = [];
//ts2
$P[] = [0];
$S[] = [[1,103.190939795922],[2,105.297198378469],[3,105.829786652111]];
$F[] = [];
$FB[] = [];
//ts3
$P[] = [0];
$S[] = [[1,107.285278217373],[2,103.557795069809],[3,105.686758569246],[4,103.748341353355]];
$F[] = [];
$FB[] = [];

I need to shuffle the first 12 blocks and then shuffle block 13-24. The code I have does not appear to work as it still shuffles all 24 blocks at once. I'm not sure how it should be written otherwise..

// DATA INITIALISATION
// - Reads all data sets from server
// - Generates list of objects
// - Randomises list of objects
function DataInit1()
{
    SeriesList = [];
    CurrentGraph.Series = 0;

    // load all the data sets from server
    $.getJSON("datademo.php",
        function(Data1)
        {
            for(var i=0; i<Data1.length; i+=4)
            {
                var P = Data1[i+0];
                var S = Data1[i+1];
                var F = Data1[i+2];
                var FB = Data1[i+3];
                var NewSeries = new SeriesClass(P,S,F,FB);
                NewSeries.SeriesNumber = (i/4)+1;
                SeriesList.push(NewSeries);
            }
        }
    );
// shuffle each of the series lists to a random order
    s1 = SeriesList.length/2;
    s2 = SeriesList.length;
    for(var i=0; i<s1; i++) 
    {
           var j = Math.floor(Math.random() * (i+1));
           var x = SeriesList[i];
           SeriesList[i] = SeriesList[j];
           SeriesList[j] = x;
         }
    for(var i=s1; i<s2; i++) 
    {
           var j = Math.floor(Math.random() * (i+1));
           var x = SeriesList[i];
           SeriesList[i] = SeriesList[j];
           SeriesList[j] = x;
         }
    }

edit: I've changed it now to the following (not pretty but I don't have time to tidy it). Now it randomizes series 1-12, but series 13-24 is now not randomized. I don't code that often and I can't see why it would work in the first bit but not the second one.

// shuffle the series list to a random order
    for(var i=SeriesList.length-13; i>0; i--) 
    {
        var j = Math.floor(Math.random() * (i+1));
        var x = SeriesList[i];
        SeriesList[i] = SeriesList[j];
        SeriesList[j] = x;
    }
        for(var i={from: 12, to: 23}; i>0; i--) 
    {
        var j = Math.floor(Math.random() * (i+1));
        var x = SeriesList[i];
        SeriesList[i] = SeriesList[j];
        SeriesList[j] = x;
    }

I think the easiest way is to break it into two arrays, shuffle them separately, then push them back together. I've used the shuffle method from this question: How can I shuffle an array? , and the method of joining arrays from How to extend an existing JavaScript array with another array, without creating a new array?

See the fiddle at https://jsfiddle.net/u25ta64x/1/

var SeriesList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];

var firstHalf = SeriesList.slice(0,13);
var secondHalf = SeriesList.slice(13,26);

function shuffle(a) {
    var j, x, i;
    for (i = a.length; i; i--) {
        j = Math.floor(Math.random() * i);
        x = a[i - 1];
        a[i - 1] = a[j];
        a[j] = x;
    }
}

shuffle(firstHalf);
shuffle(secondHalf)
var shuffledList = firstHalf;
Array.prototype.push.apply(shuffledList, secondHalf);

console.log(shuffledList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM