在1..n范围内找到具有均匀概率的k个非连续随机数(Finding k non contiguous random numbers with uniform probability in the range 1..n)
我试图在
1..n
范围内找到k
随机数,这样k
个数都不是连续的。 我想出的代码是def noncontiguoussample(n,k): import random numbers = range(n) samples = [] for _ in range(k): v = random.choice(numbers) samples.append(v) for v in range(v-1, v+2): try: numbers.remove(v) except ValueError: pass return samples
更新:我知道这个函数不会以均匀的概率返回样本。 基于我的有限测试,下面的Amber解决方案满足条件(a)样本的各个元素是非连续的,以及(b)以均匀概率生成所有可能的k个样本(来自1 ... n)。
I am trying to find
k
random numbers in the range1..n
such that none of thek
numbers are contiguous. The code I came up with isdef noncontiguoussample(n,k): import random numbers = range(n) samples = [] for _ in range(k): v = random.choice(numbers) samples.append(v) for v in range(v-1, v+2): try: numbers.remove(v) except ValueError: pass return samples
Update: I know this function won't return the samples with uniform probability. Based on my limited testing, Amber's solution below satisfies both the condition (a) individual elements of the sample are non-contiguous, and (b) all possible k samples (from 1...n) are generated with uniform probability.
原文:https://stackoverflow.com/questions/12615869
最满意答案
所以你可能不得不创建一个控制器,理想情况下是一个服务。
如果你不知道那些是什么,请查看这篇文章 -应该给你一个很好的介绍AngularJS。
好的,现在你想要实际创建一个新的控制器并将其附加到你的app模块。 您实际上可以继续并在控制器中输入pikaday函数,并通过在您的指令中在restrict下添加另一个参数将您的指令链接到控制器来调用它:
return { template: template, restrict: 'E', controller: 'controller', controllerAs: 'ctrl' }
完成后,您可以继续将pikaday函数附加到控制器,并通过在元素中添加此标记来在指令中访问它:
"ng-click='loadPikaday()'"
如果这对你来说真的让人感到困惑,我建议给我上面标记的那篇文章一个好的阅读和你应该清理的东西。 那篇文章真正概述了AngularJS应用程序的构建以及不同组件如何协同工作。
希望这有助于!
编辑:侧面说明,操作控制器中的DOM通常是不好的做法,所以你应该将pikaday函数转换为服务,但为了快速修复,这应该可行。
So you're probably going to have to create a controller and ideally a service.
If you don't know what those are, check this article out- should give you a good intro into AngularJS.
Ok, so now you'll want to actually create a new controller and attach it to your app module. You could actually go ahead and toss in the pikaday function in the controller, and call it by linking your directive to your controller by adding another parameter in your directive under restrict as:
return { template: template, restrict: 'E', controller: 'controller', controllerAs: 'ctrl' }
Once you have that in place, you can go ahead and attach the pikaday function to your controller and access it in your directive by adding this tag in your element:
"ng-click='loadPikaday()'"
If this seems really confusing to you, I'd recommend giving that article I tagged above a good read and things you should clear up a little. That article really outlines the build of AngularJS apps and how the different components work together.
Hope this helped!
EDIT: On a side note, it's generally bad practice to manipulate the DOM in controllers, so you should the pikaday function into a service, but for a quick fix, this should work.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Angular 2 - 如何访问指令并调用其中的成员函数(Angular 2 - how to access directive and call a member function in it)[2022-04-27]
这是古老的方式, @Directive({ selector: 'someDirective' }) export class SomeDirective{ constructor() {} render() { console.log('hi from directive'); } } import {Component,ViewChild} from 'angular2/core'; import {SomeDirectiv ... -
Angular:调用控制器函数里面的一个指令链接功能使用&(Angular: calling controller function inside a directive link function using &)[2023-02-09]
你是否传递{}的参数? 例如,在指令的链接函数内,你将要调用这样的方法: scope.someCtrlFn({arg1: someValue}); app.directive('myDirective', function() { return { scope: { someCtrlFn: '&callbackFn' }, link: function(scope ... -
所以你可能不得不创建一个控制器,理想情况下是一个服务。 如果你不知道那些是什么,请查看这篇文章 -应该给你一个很好的介绍AngularJS。 好的,现在你想要实际创建一个新的控制器并将其附加到你的app模块。 您实际上可以继续并在控制器中输入pikaday函数,并通过在您的指令中在restrict下添加另一个参数将您的指令链接到控制器来调用它: return { template: template, restrict: 'E', controller: 'c ...
-
删除function并用() =>替换function () @Directive ({ selector: '[bootstrap-application-wizard]', providers:[DashboardService] }) export class BootstrapApplicationWizard { $el: any; constructor(privat ...
-
您需要将test()方法从控制器传递到指令...{{project}}
-
您应该在link函数中获取activityLog范围。 模板是为了你有html模板,如果有的话。 function (angular, jQuery, oModule) { 'use strict'; oModule.directive('myDirective', function () { return { restrict: 'E', scope: { activityLog: '@', ...
-
如果你引用了JQuery,Angular会使用JQuery。 如果你不这样做,那么它就会落在一个名为JQuery Lite的更简洁的JQuery版本上。 link函数的elem参数已经是一个JQuery包装对象,表示你的指令所附加的元素。 只需从那里调用插件,它应该工作正常。 最好避免使用经典的JQuery选择器来导航DOM,而是依靠Angular来提供所需的元素。 确保在脚本引用中在 Angular 之前引用了JQuery。 app.directive('dataTableDirective', func ...
-
Angular调用父函数,其参数来自指令两级深(Angular call parent function with arguments from directive two levels deep)[2022-06-27]
https://plnkr.co/edit/WvVcNIOJ8zKvFmchOQ5R?p=preview https://plnkr.co/edit/WvVcNIOJ8 ... -
如何从Angular上的控制器调用指令的函数(How to call a directive's function from the controller on Angular)[2023-07-28]
最佳解决方案和角度方式 - 使用event 。 关于jsfiddle的实例 。 angular.module('ExampleApp', []) .controller('ExampleOneController', function($scope) { $scope.raise = function(val){ $scope.$broadcast('raise.event',val); }; }) .controller('ExampleTwoControll ...