mardi 24 septembre 2019

Range of values to populate randomly in loop

I am trying to write a method that will generate random integers in sorted order, and the limit of the random numbers should be log of how many are entered. The code I have written works when I do 10 integers, I get 0 for all the outputs which is correct. However, if I enter 100 or 1000 I don't get the values 0,1 or 0,1, and 2. I don't know why it isn't computing the correct way, I just need it to only initializes the integers in the array to be log of the number, but it doesn't work for anything greater than 10. Any help would be greatly appreciated, I have posted the code below.

public static void randomSortedLimited(ArrayList<Integer> inL,
                                                    int inHowMany) {

    inL.clear();
    int limit = (int) (Math.log10(inHowMany));
    //System.out.println(limit); added to make sure limit is calculated properly
    inL.add(0);
    for (int i = 1; i < inHowMany - 1; i++) {
        inL.add(i, inL.get(i-1) + new java.util.Random().nextInt(limit));
     }
}



Need help implementing total random characters in C

I am creating a program that prints out 1000 random occurrences of the letters "H" and "T" and my code implements rand()%2 but as i can see from running this program (code below) that its not completely random (the letters are random but always the same with every execution). I want to establish a more effective way by implementing RANDMAX to make every case of the program running completely random, how would I be able to do this?

#include <stdio.h>
#include<stdlib.h>

int main()
{
  int i=1,n;
  char ch ;
    for( i = 1; i <= 1000 ; i ++ )
    {
     n = rand()%2;
     if(n==0)
       ch = 'T';
     else
       ch = 'H';
       putchar(ch);
     }
      return 0;
  }



How do I take a random number as a pivot in a qicksort?

So I tested this code and it works if the pivot is the last element of the array, but if I try to run it with pivot being a random element the resulting array doesn't contain some of the elements of the original array

public static void quickSort(int[] S){
    int n = S.length;
    if(n<2)
        return;
    int random = (int)(Math.random() * n);

    int pivot = S[random];
    int m = 0, k = n;
    int[] temp = new int[n];

    for(int i = 0; i < n-1; i++){
        if(S[i] < pivot)
            temp[m++] = S[i];
        else if(S[i] > pivot)
            temp[--k] = S[i];
    } 
    int[] L = Arrays.copyOfRange(temp,0,m);
    int[] E = new int[k-m];
    Arrays.fill(E,pivot);
    int[] G = Arrays.copyOfRange(temp,k,n);
    quickSort(L);
    quickSort(G);
    System.arraycopy(L,0,S,0,m);
    System.arraycopy(E,0,S,m,k-m);
    System.arraycopy(G,0,S,k,n-k);
}

This code outputs 1 1 2 2 2 43




how can I rename a folder randomly each time a drive is mounted (in linux)

I have a folder on one of my hard drives that I want to rename to a different name each time I mount that drive. I currently use linux-mint 19. This drive is not mounted when I boot the machine, but when I open my file explorer and click it, it automatically mounts now. I'm not sure where to start with this... Random characters are fine, numbers or letters it does not matter.




Implementation of 2-choice algorithm for load balacing

This image specifies the problem

So far all I could come up with is:

    for task in n:
      for i in range(d):
        servers.append(random_server)

      if d > 1:
        index = min(servers)

      servers[index].taskCount++
      n--



sampling cells from matrix rows based on cell values

a 10x10 matrix contains "likelihoods" for any cell being selected in a given row during a draw.

        id1 id2 id3 id4 id5 id6 id7 id8 id9 id10
id1     NA  0.5 0.7 0.5 0.5 0.4 0.4 0.4 0.4 0.4
id2     0.5 NA  0.5 0.5 0.5 0.4 0.4 0.4 0.4 0.4
id3     0.7 0.5 NA  0.5 0.5 0.4 0.4 0.4 0.4 0.4
id4     0.5 0.5 0.5 NA  0.5 0.4 0.4 0.4 0.4 0.4
id5     0.5 0.5 0.5 0.5 NA  0.4 0.4 0.4 0.4 0.4
id6     0.4 0.4 0.4 0.4 0.4 NA  0.5 0.7 0.5 0.5
id7     0.4 0.4 0.4 0.4 0.4 0.5 NA  0.5 0.5 0.5
id8     0.4 0.4 0.4 0.4 0.4 0.7 0.5 NA  0.5 0.5
id9     0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 NA  0.5
id10    0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 NA

Each draw is done by row, and the chance of a cell being chosen is the value of that cell divided by the sum of all cell values in a given row. For example, I need to pick a cell from id2 to id10 in the row id1. The most likely choice is id3 because its value of 0.7 is the highest in the row.

I need a vector called result that stores the choice for each row after I choose. My current plan is to:

  1. sum across rows and store the results as a vector denom
  2. generate a random uniform variable between 0 and this sum for each row
  3. if the value is between 0.0 and 0.5, the chosen person in row 1 is id2; if 0.51-1.20, the chosen person is id3...etc.

This is obviously way too much work. What's a better way to sample with weights while ignoring the NA values in the diagonal?




I want to randomize letters in words, from my input

I would like to know how to change up words a bit from my textarea input. I wouldn't want all the words in the sentence to be scrambled, rather only (some) of the letters in (some) of the words. The word order has to be the same.

I was thinking of using var string_array = string.split(""); but I couldn't find a lot of documentation on this and I couldn't find any other options that would be good for me, either.

Does anybody have suggestions in how to do this?

<!--Made by MysteriousDuck#5764-->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="textchanger.js"></script>
    <title>Text changer</title>
</head>

<body>

    <div class="container">

        <h1> Text Changer </h1>
        <h2> CAPS text changer</h2>

        <textarea type="text" autofocus="true" placeholder="input text" id="inputText" value="Input Value"
            spellcheck="false"></textarea>
        <button class="button button1" onclick="myConvertFunction()">Convert</button>
        <textarea type="text" placeholder="CoNvErTeD tExT" id="converted" value="Clear" readonly="true"
            spellcheck="false"></textarea>
        <button class="button button1" onclick="myCopyFunction(); eraseText();">Copy</button>

    </div>
</body>

</html>
/* Made by MysteriousDuck#5764 */

function myConvertFunction() {
    var x = document.getElementById("inputText").value;
    var foo = x.split("");
    var string = "";
    for (i = 0; i < foo.length; i++) {
        if (i % 2 == 0) {
            string += foo[i].toUpperCase();
        } else {
            string += foo[i];
        }
    }

    document.getElementById("converted").value = string;
}

function myCopyFunction() {
    var copyText = document.getElementById("converted");
    copyText.select();
    document.execCommand("copy");
    alert("Copied the text: " + copyText.value);
    eraseText();
}

function eraseText() {
    document.getElementById("converted").value = "";
    document.getElementById("inputText").value = "";
    document.getElementById("inputText").focus();
}

function randomizeLetters() {
    var x = document.getElementById("inputText").value;
}