mardi 25 avril 2017

python 2 vs python 3 performance of random, particularly `random.sample` and `random.shuffle`

The issue of the performance of the python random module, and in particular, random.sample and random.shuffle came up in this question. On my computer, I get the following results:

> python  -m timeit -s 'import random' 'random.randint(0,1000)'
1000000 loops, best of 3: 1.07 usec per loop
> python3 -m timeit -s 'import random' 'random.randint(0,1000)'
1000000 loops, best of 3: 1.3 usec per loop

That's more than a 20% degradation of performance in python3 vs python2. It gets much worse.

> python  -m timeit -s 'import random' 'random.shuffle(list(range(10)))'
100000 loops, best of 3: 3.85 usec per loop
> python3 -m timeit -s 'import random' 'random.shuffle(list(range(10)))'
100000 loops, best of 3: 8.04 usec per loop

> python  -m timeit -s 'import random' 'random.sample(range(10),3)'
100000 loops, best of 3: 2.4 usec per loop
> python3 -m timeit -s 'import random' 'random.sample(range(10),3)'
100000 loops, best of 3: 6.49 usec per loop

That represents a 100% degradation in performance for random.shuffle, and almost a 200% degradation for random.sample. That is quite severe.


I used python 2.7.9 and python 3.4.2 in the above tests.

My question: What happened to the random module in python3?




Python array calculation

I have two array a=[0.2,0.3,0.5,0.8] b=[10,4,5] I need to: 1- create random number and choose the number which bigger than the random number created and repeat this operation 10 times as from array b[0]. for example: if I got random number = 0.1 then the result number from array a is o.2 and when done 10 times we found that 0.2 repeated 3 times.

2- Then calculate and print how much the repetitive of the chosen number in step one when repeated it 10 times

3- Repeat the operation 4 time as from array b[1] and so on for b[2] 5 times




Javascript - Multiple, Unequal Random Numbers

I am trying to write a function which produces four unequal random numbers in a given range, but the function is currently failing at the while (selection[i] in selection.slice().splice(i) line. This line should check whether the current (i'th) value is shared by any of the other random values but at the moment it seems to do nothing - perhaps I have used in incorrectly? Any help would be appreciated.

function contains(a, obj) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] === obj) {
    return true;
    }
  }
  return false;
}

selected=[];

function randomSelection() {

  var notselected=[];
  for (var i=0; i<25; i++) {
    if(!contains(selected, i)) {
        notselected.push(i);
    }
  }
  var selection=[notselected[Math.floor(Math.random() * notselected.length)],
                             notselected[Math.floor(Math.random() * notselected.length)],
                             notselected[Math.floor(Math.random() * notselected.length)],
                             notselected[Math.floor(Math.random() * notselected.length)]];

  for (var i=0; i<selection.length; i++) {
    while (selection[i] in selection.slice().splice(i)) {
      alert('Hello!')
      selection[i] = notselected[Math.floor(Math.random() * notselected.length)];
    }
  }
  for (var i=0; i<selection.length; i++) {
    selected.pop(selection[i]);
  }
}




Issue with srand() (What is valid input to it? (Number of bits?))

I am having trouble using srand() to send a seed to rand().

The problem is that the routine (C, VS) takes an unsigned int, which to me means any input from 0 to 4.3 billion (or thereabouts).

I send it regularly an input which cycles rapidly from 0 to 4.3 billion. (Intel Time Stamp Cycle, masked to 32 bits).

What I am finding is that the srand() very often produced the same result for rand() despite my novel inputs. This is making me think that the seed should ACTUALLY be something less that 32 bits, and that my large value for some reason looks the same much of the time to the rand() function. I've looked over the source code for srand() and rand() and it is unclear why a large seed input would be problematic. I am looking for an explanation of what is going on and/or some documented recommendation for seed size for srand().




Java Random Circles

I am new in Java. I want to make simple agario game for my project. But I have a problem. I want to make random stopping circles on Panel but my circles don't stop and changing.

The Problem is I think Timer.

class TestPanel extends JPanel implements ActionListener{
TestPanel(){
    Timer t = new Timer(50,this);  
    t.start();
}

Random rnd = new Random();


int r = rnd.nextInt(256);   
int b = rnd.nextInt(256);
int gr = rnd.nextInt(256);

Color randomColor = new Color(r,b,gr);

Ellipse2D.Double ball = new Ellipse2D.Double(0, 0, 40, 40);   
double v = 10;  
public void paintComponent(Graphics g){
    super.paintComponent(g); 

    Graphics2D g2 =(Graphics2D)g;


    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);


    g2.setColor(Color.RED);
    g2.fill(ball);    

    int NumOfCircles = 70;  
    int diameter;
    int x, y;


    Graphics2D g3 =(Graphics2D)g;



    for(int count = 0; count < NumOfCircles; count++){
        diameter = rnd.nextInt(10);
        x = rnd.nextInt(600);
        y = rnd.nextInt(620);
        g3.setColor(randomColor);
        g3.fillOval(x, y, diameter, diameter);
    }

    }


@Override
public void actionPerformed(ActionEvent arg0) {
    Point p = getMousePosition();
    if(p==null) return;
    double dx = p.x - ball.x - 20;
    double dy = p.y - ball.y - 20;
    if(dx*dx+dy*dy >12){
    double a=Math.atan2(dy, dx);
    ball.x += v * Math.cos(a);
    ball.y += v * Math.sin(a);}
    repaint();
}

}




Choosing a random element from a list [duplicate]

This question already has an answer here:

How can I choose a random element from a list in Python 3 ?

l=[1,2.1,"c",[4]]     #for example

Now I want to use some functions, method or whatever, that will return a random element every time I apply it to l.




Shuffle ng-repeat results

I have some data in Firebase and I'm puting it on my website and filter them. I want to show just 4 random items with property "cheap: true". Unfortunetaly I don't have any idea how can I shuffle this output. I created random function, but it causes an error:

[$rootScope:infdig]

I'd be very grateful if you could help me with shuffling these 4 results. Here is my code:

HTML:

<div ng-repeat="place in places | filter: {cheap: 'true'} | limitTo: 4 | orderBy: random" class="col-lg-3">

JS:

// Random order by
    $scope.random = function() {
      return 0.5 - Math.random();  
    };

And my Firebase data is here:

// Firebase
    $scope.places = $firebaseArray(ref);