lundi 24 juillet 2017

Is this shuffle random by property

def shuffle(deck):
    for i in range(len(deck)):
        idxToPlaceHere = randint(i, len(deck) -1)
        swap(deck, i, idxToPlaceHere);

I know theres a standard random shuffle algo on here among other sites, but for some reason this is the natural way I write shuffle. It seems to me to maintain (len-1)! possibilities. I'm new at this stuff though so can someone please confirm whether or not this is random so I know if I must memorize the standard way of doing it so as to not bomb my interview. Thanks.




How to shuffle a collection of integers so that the output is same in both C++ and Java?

I am porting a piece of code from C++ to Java. In C++, I shuffle a vector<int> as below:

std::vector<int> integers(10); //Size is not constant and can change.
std::iota(std::begin(std::begin(integers), std::end(integers), 0); //fills the vector with integers from 0 to 9
std::mt19937 gen;
gen.seed(integers.size());
std::shuffle(std::begin(integers), std::end(integers), gen);

After doing some reseach, I found out that, java uses Linear Congruential Generator (LCG) to generate random numbers. In order to use same parameters used by Java.util.random for LCG, I changed my random number generator engine to (According to this wikepedia page LCG):

std::linear_congruential_engine<unsigned long, 0x5DEECE66D, 11, 281474976710655>  engine(10);

C++ shuffled result : 6,9,0,3,8,4,1,2,7,5

Here is the correspoding Java code:

ArrayList<Integer> array = new ArrayList(10);
for(int i = 0; i < 10; ++i){
    array.add(i, i);
}
Collections.shuffle(array,  new Random(10));
for(int i = 0; i < 10; ++i){
    System.out.print(array.get(i));
}

Java shuffled result: 9,7,8,0,1,4,5,2,6,3

  1. Is there a way to make C++ stadard random number generators to behave the same way as of Java or vice versa?
  2. Is there any other (probably simpler) approach I could take to solve this problem? I don't mind changing my existing C++ code.



Getting int object is not iterable error

I'm using pyautogui to type in a textbox and I'm trying to type a random integer something like this.

import pyautogui
import random
pyautogui.typewrite(random.randint(0, 1000)

but I'm getting a 'int object is not iterable' error. Is there any way to fix this?




random pop from dict, serving values, not keys

            try:
                item = word_bank.pop(random.choice(list(word_bank.keys()))) 
                print(list(word_bank.keys()))
                print("The Word is: ", item)
                print("=" * 25)
                a = input("Do You Know It? (y/n):\t")
                answer = word_bank[item]

The my item variable above is returning values instead of keys from my word_bank dictionary. I printed my list of keys to try and debug the problem and no values exist, which makes me think the problem exists with the .pop method or the random.choice method. I looked into both the documentation for these and found nothing that would point out the issue.

I think it has something to do with random.choices returning an index to a key and then the pop method returning the corresponding value to that key as item.. However, when I reverse the roles and use values() in place of keys() I get a key error and cannot find a working solution to the problem.

Could anyone please shed some light on this, and help me find the most efficient way to do this?

thanks in advance




Generate a random Alphanumeric String without some characters

I want to generate a random Alphanumeric String. I want to exlude some characters from my string

l, i, o and the number 0

For the moment i have this code:

numberFile = RandomStringUtils.randomAlphanumeric( 5 );




Transforming draws in Matlab from Gaussian mixture to uniform

Consider the following draws for a 2x1 vector in Matlab with a probability distribution that is a mixture of two Gaussian components.

P=10^3; %number draws
v=0.038462;

%First component
mu_a = [0,0.2806];
sigma_a = [v,0;0,v];

%Second component
mu_b = [0,-1.6806];
sigma_b = [v,0;0,v];


%Combine    
MU = [mu_a;mu_b];
SIGMA = cat(3,sigma_a,sigma_b);
w = ones(1,2)/2; %equal weight 0.5
obj = gmdistribution(MU,SIGMA,w);

%Draws
RV_temp = random(obj,P);%Px2

% Transform each component of RV_temp into a uniform in [0,1] by estimating the cdf.
RV1=ksdensity(RV_temp(:,1), RV_temp(:,1),'function', 'cdf');
RV2=ksdensity(RV_temp(:,2), RV_temp(:,2),'function', 'cdf'); 

Now, if we check whether RV1 and RV2 are uniformly distributed on [0,1] by doing

ecdf(RV1)
ecdf(RV2)

we can see that RV1 is uniformly distributed on [0,1] (the empirical cdf is close to the 45 degree line) while RV2 is not.

Could you help me to understand why?




How to randomize the order of three divs that are randomly generated with no repeat onclick?

I have a code that randomly shows three divs with no repeat on click that are selected by their id's. Right now it is being used for playing cards, but I would like to retain it's current functionality of showing the elements by id rather than any other method because it can be repurposed for showing other kinds of elements and images etc. and can be styled in CSS.

Right now the code is working nearly the way I would like it. The only problem is that it shows the cards in the order they are written in html, i.e:

element1, element2, element3...element4, etc.

returning something like:

element9, element27, element48

(for example) when I run the function. I would like the three elements to be generated in random order irrespective of their sequence coded in html, so it would be possible to generate the three divs in a sequence such as

element54, element21, element36 

Does someone know how to modify the following code in order to do so?

Jquery:

var myarray = [                    
"#card1","#card2","#card3","#card4","#card5","#card6","#card7","#card8","#card9","#card10","#card11","#card12","#card13","#card14","#card15","#card16","#card17","#card18","#card19","#card20","#card21","#card22","#card23","#card24","#card25","#card26","#card27","#card28","#card29","#card30","#card31","#card32","#card33","#card34","#card35","#card36","#card37","#card38","#card39","#card40","#card41","#card42","#card43","#card44","#card45","#card46","#card47","#card48","#card49","#card50","#card51","#card52","#card53","#card54"
];
var numberOfCards = 3;
$(".cards").hide();

var previous = [];

function getRandom() {

   if(myarray.length<3){
       myarray =     ["#card1","#card2","#card3","#card4","#card5","#card6","#card7","#card8","#card9","#card10","#card11","#card12","#card13","#card14","#card15","#card16","#card17","#card18","#card19","#card20","#card21","#card22","#card23","#card24","#card25","#card26","#card27","#card28","#card29","#card30","#card31","#card32","#card33","#card34","#card35","#card36","#card37","#card38","#card39","#card40","#card41","#card42","#card43","#card44","#card45","#card46","#card47","#card48","#card49","#card50","#card51","#card52","#card53","#card54"
];
   }

   for (var i = 1; i <= numberOfCards; i++) {
       var randomIndex = RandomDiv();
       previous.push(myarray[randomIndex]);
       $(myarray[randomIndex]).fadeIn(900).css('display', 'inline-block');
       myarray.splice(randomIndex, 1);
   }
};

$('.contact-btn').on('click', function() {
   for(k=0; k< numberOfCards; k++){
   $(previous[k]).hide();
   }
   previous = [];
   getRandom();
});

function RandomDiv() {
    return Math.floor(Math.random() * myarray.length);
}

Html (this is just a snippet, the full code goes up to 54 cards):

<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href=style.css>
    <script type="text/javascript" src="http://ift.tt/2nYZfvi"></script>
    <script type"text/javascript" src="js/main.js"></script>
  </head>
    <div class="card">
      <div style="font-size: 24px" style="display; inline-block">       
        <div class="cards" id="card1"><span style="color: #C23B22">2♦</span></div>
        <div class="cards" id="card2"><span style="color: #C23B22">2♥</span></div>
        <div class="cards"id="card3">2♣</div>
        <div class="cards" id="car4">2♠</div>
        <div class="cards" id="card5"><span style="color: #C23B22">3♦</span></div>
        <div class="cards" id="card6"><span style="color: #C23B22">3♥</span></div>
        <div class="cards" id="card7">3♠</div>
        <div class="cards" id="card8">3♣</div>
        <div class="cards" id="card9"><span style="color: #C23B22">4♦</span></div>
        <div class="cards" id="card10"><span style="color:#C23B22">4♥</span></div>
        <div class="cards" id="card11">4♣</div>
        <div class="cards" id="card12">4♠</div>
        <div class="cards" id="card13"><span style="color:#C23B22">5♦</span></div>
        <div class="cards" id="card14"><span style="color:#C23B22">5♥</span></div>
        <div class="cards" id="card15">5♣</div>
    ...
  </div>
</div>
<input type="button" id="Button" value="Random" onclick="RandomDiv();" />

CSS:

cards {
display: none;
}

I would also like to keep it fading in at that rate. I know that the code is picking three random cards and displaying them in the order they are written, so I would like any suggestions on changing the code/modifying the function so it shows/generates the three cards/elements in random order as well! Thank you.