Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def sample(n, lower, upper):
- result = []
- pool = {}
- for _ in xrange(n):
- i = random.randint(lower, upper)
- x = pool.get(i, i)
- pool[i] = pool.get(lower, lower)
- lower += 1
- result.append(x)
- return result
- import random
- print(lambda x,y,z:random.sample(xrange(y,z),x))(input(),input(),input())
- python sample.py
- 10
- 0
- 2**31-1
- [786475923, 2087214992, 951609341, 1894308203, 173531663, 211170399, 426989602, 1909298419, 1424337410, 2090382873]
- #include <stdlib.h>
- #include <stdint.h>
- #define MAX_ALLOC ((uint32_t)0x40000000) //max allocated bytes, fix per platform
- #define MAX_SAMPLES (MAX_ALLOC/sizeof(uint32_t))
- int* randsamp(uint32_t x, uint32_t min, uint32_t max)
- {
- uint32_t r,i=x,*a;
- if (!x||x>MAX_SAMPLES||x>(max-min+1)) return NULL;
- a=malloc(x*sizeof(uint32_t));
- while (i--) {
- r= (max-min+1-i);
- a[i]=min+=(r ? rand()%r : 0);
- min++;
- }
- while (x>1) {
- r=a[i=rand()%x--];
- a[i]=a[x];
- a[x]=r;
- }
- return a;
- }
- def pick(num, min, max)
- (min..max).to_a.sample(num)
- end
- p pick(5, 10, 20) #=>[12, 18, 13, 11, 10]
- s <- function(n, lower, upper) sample(lower:upper,n); s(10,0,2^31-2)
- object RandSet {
- val random = util.Random
- def rand (count: Int, lower: Int, upper: Int, sofar: Set[Int] = Set.empty): Set[Int] =
- if (count == sofar.size) sofar else
- rand (count, lower, upper, sofar + (random.nextInt (upper-lower) + lower))
- }
- object RandSetRunner {
- def main (args: Array [String]) : Unit = {
- if (args.length == 4)
- (0 until args (0).toInt).foreach { unused =>
- println (RandSet.rand (args (1).toInt, args (2).toInt, args (3).toInt).mkString (" "))
- }
- else Console.err.println ("usage: scala RandSetRunner OUTERCOUNT COUNT MIN MAX")
- }
- }
- scalac RandSetRunner.scala
- scala RandSetRunner 200 15 0 100
- time scala RandSetRunner 100000 10 0 1000000 > /dev/null
- real 0m2.728s
- user 0m2.416s
- sys 0m0.168s
- (import srfi-1) ;; for iota
- (import srfi-27) ;; randomness
- (import srfi-43) ;; for vector-swap!
- (define rand (random-source-make-integers
- default-random-source))
- ;; n: length, i: lower limit
- (define (random-range n i)
- (let ([v (list->vector (iota n i))])
- (let f ([n n])
- (let* ([i (rand n)] [n (- n 1)])
- (if (zero? n) v
- (begin (vector-swap! v n i) (f n)))))))
- random <- function(count, from, to) {
- rand.range <- to - from
- vec <- c()
- for (i in 1:count) {
- t <- sample(rand.range, 1) + from
- while(i %in% vec) {
- t <- sample(rand.range, 1) + from
- }
- vec <- c(vec, t)
- }
- return(vec)
- }
- f:{(neg x)?y+til z}
- q)ts do[100000;f[100;1;10000]]
- 2418 131456j
- a=scan();sample(a[2]:a[3],a[1])
- a=scan();sample(n[n>=a[2]&n<=a[3]],a[1])
- (a,b,n)=>_.Range(0,n).Select(x=>Math.random()*(b-a)+a).ToArray()
- # The Tree growing algorithm for uniform sampling without replacement
- # by Pavel Ruzankin
- quicksample = function (n,size)
- # n - the number of items to choose from
- # size - the sample size
- {
- s=as.integer(size)
- if (s>n) {
- stop("Sample size is greater than the number of items to choose from")
- }
- # upv=integer(s) #level up edge is pointing to
- leftv=integer(s) #left edge is poiting to; must be filled with zeros
- rightv=integer(s) #right edge is pointig to; must be filled with zeros
- samp=integer(s) #the sample
- ordn=integer(s) #relative ordinal number
- ordn[1L]=1L #initial value for the root vertex
- samp[1L]=sample(n,1L)
- if (s > 1L) for (j in 2L:s) {
- curn=sample(n-j+1L,1L) #current number sampled
- curordn=0L #currend ordinal number
- v=1L #current vertice
- from=1L #how have come here: 0 - by left edge, 1 - by right edge
- repeat {
- curordn=curordn+ordn[v]
- if (curn+curordn>samp[v]) { #going down by the right edge
- if (from == 0L) {
- ordn[v]=ordn[v]-1L
- }
- if (rightv[v]!=0L) {
- v=rightv[v]
- from=1L
- } else { #creating a new vertex
- samp[j]=curn+curordn
- ordn[j]=1L
- # upv[j]=v
- rightv[v]=j
- break
- }
- } else { #going down by the left edge
- if (from==1L) {
- ordn[v]=ordn[v]+1L
- }
- if (leftv[v]!=0L) {
- v=leftv[v]
- from=0L
- } else { #creating a new vertex
- samp[j]=curn+curordn-1L
- ordn[j]=-1L
- # upv[j]=v
- leftv[v]=j
- break
- }
- }
- }
- }
- return(samp)
- }
- int rands,elements;
- int low,up,i;
- i=0;
- scanf("%d",&elements);
- scanf("%d",&low);
- scanf("%d",&up);
- /* initialize random seed: */
- srand ( time(NULL) );
- while(1)
- { /* generate secret number: */
- rands = rand() % up ;
- if(rands>=low && rands<=up)
- {
- printf("%d ",rands);
- i++;
- }
- if(i>=elements)
- {
- break;
- }
- }
Add Comment
Please, Sign In to add comment