• NSArray+DERandomization


    #import <Foundation/Foundation.h>

    @interface NSArray (DERandomization)

    + (NSArray *)randomArrayFromArray:(NSArray *)sortedArray;

    @end

    #import "NSArray+DERandomization.h"

    @implementation NSArray (DERandomization)

    + (NSArray *)randomArrayFromArray:(NSArray *)sortedArray {

    NSMutableArray *toRandomize = [sortedArray mutableCopy];

    NSMutableArray *randomArray = [[[NSMutableArray alloc] init] autorelease];

    NSUInteger arraySize = toRandomize.count;

    NSUInteger randomIndex;

    while(arraySize > 0) {

    //Grab a random object from toRandomize and place it in first index of randomArray

    randomIndex = arc4random() % arraySize;

    [randomArray insertObject:[toRandomize objectAtIndex:randomIndex] atIndex:0];

    [toRandomize removeObjectAtIndex:randomIndex];

    arraySize = toRandomize.count;

    }

    [toRandomize release];

    return randomArray;

    }

    @end

  • 相关阅读:
    [CQOI2017] 小Q的棋盘
    CF75D Big Maximum Sum
    Dockerfile
    docker镜像与容器的导出导入
    ubuntu安装glusterFS
    常用工具网站网址
    国内数据分析平台
    清理系统垃圾
    sql注入笔记
    shopify Liquid语言学习知识点总结
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2458495.html
Copyright © 2020-2023  润新知