lundi 2 octobre 2017

Problems with JSONArray randomize

I'm trying to randomize JSON array this way:

ArrayList<Integer> intArr1 = new ArrayList<>(myJSONarr.length());
        for (int i = 0; i < myJSONarr.length(); i++) {
            intArr1.add(i);
        }
        Collections.shuffle(intArr1);
        for (int i = 0; i < intArr1.size(); i++) {
            myJSONarr.put(i, myJSONarr.getJSONObject(intArr1.get(i)));
            if (i == (myJSONarr.length() - 1))
                break;
        }

As a result of this not only JSON objects are randomizing, but also keys and values. I mean, I had an array like this:

[ {"id":"0","hieroglyph":"水","pinyin":"Shuǐ","pinyin_num":"Shui3","russian":["вода"],"hsk":"1"}, {"id":"1","hieroglyph":"人","pinyin":"Rén","pinyin_num":"Re2n","russian":["человек"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"3","hieroglyph":"不","pinyin":"Bù","pinyin_num":"Bu4","russian":["нет"],"hsk":"1"}, {"id":"4","hieroglyph":"少","pinyin":"Shǎo","pinyin_num":"Sha3o","russian":["мало"],"hsk":"1"}]

And after randomize it looks like this

[{"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"4","hieroglyph":"少","pinyin":"Shǎo","pinyin_num":"Sha3o","russian":["мало"],"hsk":"1"}, {"id":"3","hieroglyph":"不","pinyin":"Bù","pinyin_num":"Bu4","russian":["нет"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}]




math random 0 can have zeros on the right?

my point is the following, Math.random() brings me an number between 0 and 1, excluding the 1, but what happens if the algorithm bring me these:

 0.21731701170415185
 0.9203166921156665
 0.5072768945868098
 0.7774864190342448

and in one of those, would bring me this?

 0

or this

0.0000000000000000




Divide / Hand out random amount

Lets say I have 8 people and 5000 apples.
I want to hand out all the apples to all 8 people so i have no apples left.
But everyone should get a different amount

What would be the best way to give them all out?

I started of with this:

let people = 8
let apples = 5000

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
}

while (people--) {
  // last person get the rest
  let x = people ? getRandomInt(0, apples) : apples

  // subtract how many apples i got left
  apples -= x 

  console.log(`Giving person ${people + 1} ${x} apples (got ${apples} left)`)
}

But the thing I don't like about this is that the last person get very few apples (sometimes less then 5 apples) and the first person gets way more then the others




Random sequence of words "true" and "false" in python?

How to generate a random sequence made of words "true" and "false" in python (or python3)?




Getting all possible randomly generated lists in Prolog

I've written a predicate that randomly picks 4 elements from a list of 6 elements [a,b,c,d,e,f]:

?- random_code(X). X = [e, c, b, f]

?- random_code(X). X = [b, d, c, e]

?- random_code(X). X = [c, f, d, a]

etc.

What I'm trying to get is a predicate for a list of ALL possible outcomes from this query, meaning 6*5*4*3 = 320 (right?) answers, at once.




Android: URL Connection accessing to website

I am extracting details from a website using the below code.

Code:

private class FetchAllData extends AsyncTask<Void, Void, Void> 
    {
         @Override
         protected void onPreExecute() 
         {
             super.onPreExecute();              
             Utilities.custom_toast(CurrentResult.this, "Refreshing", "gone!", "short", "vertical");
         }

         @Override
         protected Void doInBackground(Void... params) 
         {
            try 
            {
                //String urlX = URL1 + "?x=" + new Random().nextInt(100000); //Method1
                String urlX = URL1 //Method2;
                URL url = new URL(""+ urlX);
                URLConnection con = url.openConnection();
                con.setUseCaches(false); //This will stop caching!
                // BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                PageCode = "";
                OriginalPageCode = "";
                while ((inputLine = in.readLine()) != null) 
                {
                    PageCode += inputLine;
                }                   
                OriginalPageCode = PageCode;
                toast_IshtmlObtained = urlX+ "\nHTML success obtained as follows:\n\n";
                try
                {
                    extract_website_and_save();
                    toast_IsInfoExtracted = "success extracting website";
                }
                catch (Exception e1)
                {
                    toast_IsInfoExtracted = "error extracting website";
                }

                in.close();
            } 
            catch (Exception e) 
            {
                PageCode = "ERROR: " + e;
                toast_IshtmlObtained = "HTML not obtained:\nHTML retrieved as follows:" + PageCode;
            }
            return null;
         }

         @Override
         protected void onPostExecute(Void result) 
         { 
             Utilities.custom_toast(CurrentResult.this, "Done", "gone!", "short", "vertical");

             setText();
             ......
         }
     }

Question:

Beforehand I was using the URL in Method1 and was successful to access and extract the website details. However, in these days it does not work. I now tried Method2 and it works now.

I would like to ask if the random number in Method1 is important if the number of users accessing to the website is enormous, and any drawback if using the direct URL as in Method2? Thanks.




How to use NumPy to create two subarrays from the randomized contents of a source array?

I have an array A, I want to create arrays B and C.

Basically, I want to randomly find 100 elements in A, put them in B and the rest in C.

If I use numpy.random.choice I can easily create B by just extracting all the elements in A that match the indices in the random list, but I would have to go through A again in order to find all the values that are not in B and put them in C. This works, but maybe there is a built-in function that can do this for me.

Is there a cheaper way?