samedi 1 août 2015

Get Quote and it's author SWIFT

I am very new to swift, and I am trying to make an app of quotes, but I want to display the Quote and its own author both in a separate UILabel, Bu I don't know how to do this, because I have a randomQuote function that returns a random index for my array but the array only displays the quote, I want to get a random quote and get the author of the quote.

Here is what I have done so far:

   var quotes = ["'Stay Hungry, Stay Foolish.'", "'Life is what happens while you are busy making other plans.'", ""]

var authors = ["Steve Jobs", "John Lennon"]



func getRandomQuote() -> String{
    //get random index
    var arrayCount = UInt32(quotes.count)
    var randomNumber = arc4random_uniform(arrayCount)
    var finalNumber = Int(randomNumber)

    return quotes[finalNumber]
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var randomImage: String = imageChange[randomNumber]
    self.motivationText.text = getRandomQuote()
    self.imageInspire.image = UIImage(named: randomImage)
}

I was thinking about using a dictionary but I think it won't work :/ Please don't be rude I am very new.




choosing circular shape of a matrix

How to choose binary random elements of a matrix so that the matrix shape can be similar to a circle.(for example, '1' means presence of that element of matrix and '0' means I do not use that element and put it equal to zero)




Remove arraylist string stopping random function

Thanks to the stackoverflow community, my problem was fixed. However, I now have a new problem. I have some code that reads a random line from a file, displays it and now removes the random line from an arraylist so that it won't be displayed again. But now, the lines being shown aren't random. I have a file with 40 lines in it and now it's only showing the last 20 lines, in order. Here's the updated code:

try {
    Random random = new Random();
    int randomInt = random.nextInt(50);
    FileReader fr = new FileReader(file);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    if (line.contains("!")) {
        List<String> lines = new ArrayList<>();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
            Random r = new Random();
            String rdmln = lines.get(r.nextInt(lines.size()));
            line1.setText("Line" + rdmln);
            lines.remove(rdmln);

The remainder of the code repeats the process from 'lines.add(line)' to 'lines.remove(rdmln)' but instead, it displays 'rdmln2' on the label 'line2', then removes 'rdmln2' and so on. Can anyone see what might be causing this problem? Thanks in advance.




Function in bash that generates random chars from /dev/random

In my .bashrc file I appended the line psswd() { LC_ALL=C tr -dc 'a-zA-Z0-9-!"@/#$%^&*()_+~' < /dev/urandom | head -c "$1";echo ;} so that when I type psswd n in the terminal, it returns a string of n random characters. I would like to achieve the same but using /dev/random instead of /dev/urandom. However when I replace /urandom by /random, calling psswd does nothing (cannot even output a single random character after 1 hour), it's as if it's frozen. I don't know why it is so, and I know it's not a problem of not having enough entropy. The reason is that the command od -An -N1 -i /dev/random returns a random number.

Note that this last command returns a random number almost instantly if I type it say after a fresh reboot. But if I have invoked a call to psswd n with /random, then the command returns a random number after about 15 seconds. So the call to /random seems to have some effect on /dev/random even though it produces no output when I call the function psswd.

Overall I'd like to know how I could create a function that uses /dev/random to generate a random string of n characters.




Python Help, Secret Message [on hold]

I have recently finished learning python from codecademy.com and have been using my newly found skills to try and convert a string into a distorted message only people with binary knowledge would understand.

import random

text = input("Phrase: ")


def t_rand():
    test = int(random.randrange(0, 1))
    if test == 1:
        return True
    elif test == 0:
        return False


for x in text:
    rand = t_rand()
    if x == "l" or x == "L":
        text.replace(x, bin(1))
    elif x == "e" or x == "E":
        text.replace(x, bin(3))
    elif x == "i" or x == "I":
        text.replace(x, bin(1))
    elif x == "t" or x == "T":
        text.replace(x, bin(7))
    elif x == "o" or x == "O":
        text.replace(x, bin(0))
    else:
        if rand:
            text.replace(x, x.upper())
        elif not rand:
            text.replace(x, x.lower())

print(text)

PS: Sorry if it is a repost




PHP - Generating random integers within specified range from a key

I have a set of questions with unique IDs in a MySQL database. Users also have a unique ID and are to answer these questions and their answers are saved in the database.

Now, I want users to get 5 non-repeating uniquely and randomly picked questions from the pool of available ones (let's say 50) based on users ID. So when a user with id 10 starts answering his questions, but stops and wants to return later to the same page, he will get the same questions as before. A user with id 11 will get a different random set of questions, but it will always be the same for him and different from all other users.

I found that random.org can generate exactly what I need with their sequence generator that generates a random sequence of numbers based on provided ID: http://ift.tt/1IB7TTC But I would like the generation to be done locally instead of relying random.org API.

So, I need to generate 'X' unique random integers, within specified range 'Y' that are generated based on supplied integer 'Z'. I should be able to call a function with 'Z' as parameter and receive back the same 'X' integers every time.

I need to know how to replicate this generation with PHP code or at least a push or hint in a direction of a PHP function, pseudo-code or code snippet that will allow me to do it myself. Thank you in advance!




Random Char in 2D arrays

Hi i am new at java coding and am trying to create random numbers(which i have done) and i am trying to assign this random numbers as coordinates into the 2D array and print 'A' at the coordinates. Any help is appreciated.

package training;

import java.util.Random;

public class Training {

    public static void main(String[] args) {

        char[][] values = new char[10][10];
        int foodX[] = new int[15];
        for (int i = 1; i < foodX.length + 1; i++) {
            int minFood = 0;
            int maxFood = 10;
            int randNum1 = minFood + (int) (Math.random() * (maxFood - minFood) + 1);
            int minFoodY = 0;
            int maxFoodY = 10;
            int randNum2 = minFoodY + (int) (Math.random() * (maxFoodY - minFoodY) + 1);
            for (int j = 1; j < foodX.length + 1; j++) {
                values[randNum1][randNum2] = 'A';

            }

        }

    // Assign three elements within it.
        // Loop over top-level arrays.
        for (int i = 0; i < values.length; i++) {

            // Loop and display sub-arrays.
            char[] sub = values[i];

            for (int x = 0; x < sub.length; x++) {
                System.out.print(sub[x] + " ");
            }
            System.out.println();

        }

    }
}