jeudi 1 novembre 2018

Generate Random number in a range with no repeatation

I am trying to randomize quiz questions from firebase. I am trying to generate random numbers from 0-50 add them in a array and once the number is used I want that number to be removed. But it goes forever with repeatation, here is piece of code that I use to generate random

 public void updateQuestion (){

    for (int i = 0; i < 50; i++) {
        questionNos.add(i);
    }
    Random r = new Random();
    index = r.nextInt(questionNos.size()-1);
    mQuestionNo = questionNos.remove(index);

and to switch activity

 if (questionNos == null ){
        Intent i = new Intent(qiozphy.this,Score.class);
        i.putExtra("Score",mScore);
        startActivity(i);

    }

I am calling updatequestion() on oncreate and 4 choice butons of the quiz.




TSQL - Random winner for multiple locations based on weight

Our company does a drawing every month for "customer of the month". The drawing is basically a weighted drawing based on how much the customer spent that month.

Currently I am getting the customer name and their amount of points and running it through an Excel randomizer I found online. This becomes cumbersome doing it individually for 40+ locations every month.

I am going to appologize becuase I have seen some other questions about this but i just cant wrap my head around writing the TSQL to choose the random weighted winner.

Obvisously it would be nice if a single command could output me a random winner for each location, but im having difficulty finding out where to start and ensuring that its choosing the weighted winner correctly.




replace randomly by python 10% of values with NaN in a csv file

I have a csv file. I want to replace randomly by python 10% of values of this file in NaN. How to do it




Is there any way to make a random number without a library [Python3]

I was wondering if there was any way to generate a random number, from 1 to 9, without using external libraries, even if they are included with Python.




How to create map with random numbers inside in c

I have task about printing board where you should input dimension and program will create a board with 2 numbers inside each cell: 1'st number is random from 1-3 and second one should be zero. I can make it only with zero but when i tried to make a random number everything went wrong way.. Maybe anyone knowns what it wrong with it?

Function calls uploading map:

int randfunc(int i, int n);

    int uploadmap(int m,int n){
    int a[m][n];
    int i,j,k;
    // time_t t;
    //srand((unsigned)time(&t));
    //int randnum =  rand() % 3 + 1;
    for(i = 0; i < m;i++){
        printf("+---");

    }
    printf("+\n");
    memset(a,0,sizeof(a));
    for(i = 0;i < m;i++){
        for(j = 0; j < n;j++){
            printf("|%d %d",randfunc(i,n),a[i][j]);
        }
        printf("|\n");
        for(k = 0;k < m;k++){
            printf("+---");

        }
        printf("+\n");
    }
    return 0;
}

function which calls random numbers from 1 to 3:

int randfunc(int i, int n) {

    time_t t;

    srand((unsigned) time(&t));

    for( i = 0 ; i < n ; i++ ) {
        printf("%d\n", rand() % 3 + 1);
    }

    return 0;
}

Main function :

int main(int argc, const char * argv[]) {
    int m,n;
    printf("Enter dimension: \n");
    scanf("%d %d",&m, &n);
    printf("Map has been uploaded %d\n",uploadmap(m,n));

    return 0;
}




DIfference in output of srand() on windows gcc and expected output for C programming?

i would like to know why there is a difference in the output from my gcc compiler in Sublime Text 3 on Windows 10 and the expected output? If so how can i change the compiler such that it operates the same?

This is the expected output, which i can receive when utilising https://repl.it/repls/InternalSeveralEntropy with gcc 4.6.3

The set of numbers are:

1 4 3 3 7 2 3 2 9 9 10 9 5 2 10 7 2 3 1 1 

However, this is the output i receive using multiple gcc versions, 5.4.0, 6.3.0, 8.1.0 and even 4.6.3.

The set of numbers are:
2 2 8 6 8 2 3 8 4 6 10 5 9 2 8 10 7 2 1 6

I have tried searching and the closest i have come to understand is that it has soemthing to do with srand() functioning differently? Attached below is my code.

    #include <stdio.h>

    #include <stdlib.h>

    int main(void) {

    int N = 20, DATA[20];

    int i; //< you local variables here >

    srand(454646); //don't change this line! Will be used in autograding... You may fail test cases if change this

    for (i = 0; i < N; i++){        //< generate rand numbers and store here in DATA array >
        DATA[i] = rand()%10 + 1;
    }

    printf("The set of numbers are:\n");
    for (i = 0; i < N; i++){
        printf("%d ", DATA[i]);
    }
    printf("\n");

    //< write using FOR loops to determine MODE and print >

    return 0;

}




mercredi 31 octobre 2018

Check if one element in array is being used, if so, use another element

How do I check if one element is being used, and if so to use another element on the Switch Case statements.

//Input
function myFunction() {
  
var myArray = ["60","50", "20", "30", "15", "10"];
  var randomJumpingJacks = myArray[Math.floor(Math.random()*myArray.length)];
  var randomCrunches = myArray[Math.floor(Math.random()*myArray.length)];
   
    var workOut = document.getElementById("myInput").value;
  
  
var text = "";
for(const char of workOut.toUpperCase()){
    switch(char) {
        case "A":
        case "I":
        case "N":
        case "X":
            text += randomJumpingJacks;
            text += " Jumping Jacks";
                  
        break;
        case "B":
        case "J":
        case "Q":
        case "Y":
            text += randomCrunches;
            text += " Crunches";       
          break;
          case " ":
            /*text += " break ";*/
        break;
       
        default:
        text += "I have never heard of that fruit...";    
    }
  text +=" "
  text +="<br>"
}
  
    document.getElementById("excercise").innerHTML = text;
}
<!DOCTYPE html>
<html>
<body>

<p>Input some characters  and click the button.</p>
<p>Your excericse routine will display based on your input.</p>

<input id="myInput" type="text">

<button onclick="myFunction()">Try it</button>
<p>
  <span id="reps"></span>
  <span id="excercise"></span>
  </p>

</body>
</html>

Example: I typed: ABBY I'm expecting it to output:

10 Jumping Jacks

15 Crunches

10 Crunches

20 Crunches

I'm unsure if I'm using the correct terminology, but I'm going to refer cases A,I,N,X as Jumping Block, and cases B,J,Q,Y as Crunches Block.

If the chars called are from the same block, there's a tendency to display the same random number.

On the second Char (B) of the Crunches block, it naturally outputs 15, but since it is being used for the first char (also B), I'd like it to use some other element,

and the third char (Y) being on the Crunches block as well, I'd like it to use a different element that are different from the first two elements, and so forth, and repeat after 4 times.

I'd love to know what you think.