vendredi 24 juillet 2015

MS Access - select one random record for each unique value in a column

I have a table of ~3,000 invoices that are from 40 different vendors. In MS Access 2013, how would I randomly pick one record from each of the 40 vendors?




calling a method in java and incrementing an array in a for loop

Hi i am new to programming, my program is a random number generator using the math.random class and i am having issues with my code. I need to Call the randInt method n times in the randTest method, each time incrementing the count of the element counts corresponding to the value returned. Basically each time a random number is generated, the counts element increases.

This is my code so far:

public class RandNumGenerator {

    public static int RandInt(){
        int n = (int) Math.random()*10;
        return n;
        }

    public static void randTest(int n){
        int [] counts = new int [10];

        for(int i=0;i<10;i++){
            RandInt();

            }
            }
    }




JAVA - How can i read random proxys from txt?

i have a File.txt in mi pc, so i know how read it sequentially using this simple method:

FileReader  fl = new FileReader("C:/Users/Silver/Desktop/proxy.txt");
        BufferedReader br = new BufferedReader(fl);

        for(;;){
        String read = br.readLine();    
        System.out.println(read);
            Thread.sleep(100);
            if (read == null) {
                System.out.println("No More proxys");
                br.close();
            }

And this read until there are no more proxys, so i wanna know an easy method to do the same but this time randomly, i read about a method called "LineNumberReader" somebody that know about this can explain to me?

Thanks so much.




Modify values in random rows of a data.table by category

I have a data.table with two columns, let's say city and score

data.table(city = sample(c("Cape Town", "New York",  "Tel Aviv"),size=15, replace = TRUE), score = sample(x=1:10, size = 15, replace=TRUE))
         city score
 1:  Tel Aviv     5
 2:  New York     5
 3:  New York     8
 4: Cape Town    10
 5:  Tel Aviv     7
 6:  New York    10
 7:  Tel Aviv     8
 8: Cape Town     2
 9:  Tel Aviv     2
10: Cape Town     2
11: Cape Town     5
12:  New York     1
13:  Tel Aviv     3
14: Cape Town     6
15:  New York     5

I want to change the score to 0 to two random rows per city (i.e., 2 rows for Tel Aviv, two for New York, etc.). Please mind that there will always be more than two rows for every city (my real data are quite large...). Ideally, I would like a solution based on data.table commands... Thank you!




Selecting Random Record for each User returned in the results

I have seen several ways of selecting random records from a table using several methods. However, my need I am sure is here, I just cannot find it. I have a query using several tables. What my end goal is is to have one random record for each user returned.

In my result set, I get users and the work items they have. What I need is to only return one random work item per user. This is where I am getting stuck. ANy assistance would be greatly appreciated.




Creating and accessing images using a for loop and if statements

Currently I am working on a simple children's counting game where you have to count the number of images shown on screen and pick the right answer to get points and win.

What I want is to be able to get a random image selected using a random number generator and depending on the number, get an image associated with it. So for this assignment I have made the image names to numbers and instead of using an if statement for all of them, I was hoping to use the number generated to be able to grab the image i wanted.

eg. int randomImage = random.Next(0,15); if the randomImage number is 6, it would pick the image named 6.png.

I was wondering if anybody knows of a way to do this. my attempt was along the lines of: pictureBox2.image = randomImage + ".png";

It might seem a little advanced but i hope somebody could help. Thank you very much




Why doesn't my definition of this function lead to the desired outcome?

Why doesn't my definition of propmt_int not work? Its original defintion was:

def prompt_int(prompt=''):
    while True:
        val = input(prompt)
        try:
            return int(val)
        except:
            print("Not a valid number, please try again")

but that didn't work either and it doesn't work when I remove the statement right at the start of my code> Im really stuck please help. here is the whole of my code:

            import random
            status = input("Are you a teacher or student? Press 1 if you are a student or 2 if you are a teacher")
            def prompt_int(prompt=''):
                while True:
                    if status == prompt_int(prompt=''):
                        val = input(prompt)
                        if val in (1,2):
                            return int(val)
                            return true
                    elif status != prompt_int(prompt=''):
                        val = input(prompt)
                        if val in (1,2,3):
                            return int(val)
                            return true
                    else:
                        print("Not a valid number, please try again")




            def get_bool_input(prompt=''):
                while True:
                    val = input(prompt).lower()
                    if val == 'yes':
                        return True
                    elif val == 'no':
                        return False
                    else:
                        print("Not a valid input (yes/no is expected) please try again")

            status = prompt_int("Are you a teacher or student? Press 1 if you are a student or 2 if you are a teacher")

            if status == "1":
                score=0
                name=input("What is your name?")
                print ("Alright",name,"welcome to your maths quiz."
                        " Remember to round all answers to 5 decimal places.")

                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                             "Press 1 for low, 2 for intermediate "
                                                "or 3 for high\n")))
                if level_of_difficulty not in (1,2,3):
                    sys.exit("That is not a valid level of difficulty, please try again")

                if level_of_difficulty == 3:
                    ops = ['+', '-', '*', '/']
                else:
                    ops = ['+', '-', '*']

                for question_num in range(1, 11):
                    if level_of_difficulty == 1:
                        number_1 = random.randrange(1, 10)
                        number_2 = random.randrange(1, 10)
                    else:
                        number_1 = random.randrange(1, 20)
                        number_2 = random.randrange(1, 20)

                    operation = random.choice(ops)
                    maths = round(eval(str(number_1) + operation + str(number_2)),5)
                    print('\nQuestion number: {}'.format(question_num))
                    print ("The question is",number_1,operation,number_2)

                    answer = float(input("What is your answer: "))
                    if answer == maths:
                        print("Correct")
                        score = score + 1
                    else:
                        print ("Incorrect. The actual answer is",maths)

                if score >5:
                    print("Well done you scored",score,"out of 10")
                else:
                    print("Unfortunately you only scored",score,"out of 10. Better luck next time")

                class_number = prompt_int("Before your score is saved ,are you in class 1, 2 or 3? Press the matching number")

                filename = (class_number + "txt")

                with open(filename, 'a') as f:
                    f.write("\n" + str(name) + " scored " + str(score) +  " on difficulty level " + str(level_of_difficulty + "\n"))

                with open(filename, 'a') as f:
                    f = open(filename, "r")
                    lines = [line for line in f if line.strip()]
                    f.close()
                    lines.sort()

                if get_bool_input("Do you wish to view previous results for your class"):
                    for line in lines:
                        print (line)
                else:
                    sys.exit("Thanks for taking part in the quiz, your teacher should discuss your score with you later")

            if status == "2":
                class_number = prompt_int("Which classes scores would you like to see? Press 1 for class 1, 2 for class 2 or 3 for class 3")
                filename = (class_number + "txt")
                with open(filename, 'a') as f:
                    f = open(filename, "r")
                    lines = [line for line in f if line.strip()]
                    f.close()
                    lines.sort()
                    for line in lines:
                        print (line)