-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathJFRandom.m
executable file
·347 lines (286 loc) · 7.91 KB
/
JFRandom.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// JFRandom.m
//
// Created by Jason Fuerstenberg on 10/04/26.
// Copyright 2010 Jason Fuerstenberg
//
// http://www.jayfuerstenberg.com
// jay@jayfuerstenberg.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "JFRandom.h"
@implementation JFRandom
/*
* Generates a random number between low and high and places it into the receiver.
*
* Params
* low The low number.
* Must be lower or equal to high.
* high The high number.
* Must be equal or higher than low.
* receiver The receiver for the random number.
* Must not be nil.
*
* Return
* YES if successful, NO otherwise.
*/
+ (BOOL) generateNumberBetweenLow: (NSInteger) low andHigh: (NSInteger) high intoReceiver: (NSInteger *) receiver {
if (low > high) {
return NO;
}
if (receiver == nil) {
return NO;
}
*receiver = (arc4random() % ((high + 1) - low)) + low;
return YES;
}
/*
* Generates an optionally unique sequence of random numbers between low and high and places them into the sequence.
*
* Params
* length The length of the sequence.
* Must be at least 1.
* sequence The receiving sequence.
* Must not be nil and must accomodate at least 'length' numbers.
* low The low number.
* Must be lower or equal to high.
* high The high number.
* Must be equal or higher than low.
* onlyUniqueValues YES if only unique values are to be generated, NO otherwise.
*
* Return
* YES if successful, NO otherwise.
*/
+ (BOOL) generateNumberSequenceOfLength: (NSUInteger) length into: (NSInteger[]) sequence betweenLow: (NSInteger) low andHigh: (NSInteger) high withOnlyUniqueValues: (BOOL) onlyUniqueValues {
if (length < 1) {
return NO;
}
if (sequence == nil) {
return NO;
}
if (low > high) {
return NO;
}
if (onlyUniqueValues && length > (high - low) + 1) {
return NO;
}
NSInteger number;
if (onlyUniqueValues) {
for (NSUInteger loop = 0; loop < length; loop++) {
// Only unique values are allowed.
if (![JFRandom generateNumberBetweenLow: low
andHigh: high
intoReceiver: &number]) {
return NO;
}
if ([JFRandom isNumber: number
inSequence: sequence
ofLength: loop]) {
// This is not a unique number so skip adding it to the sequence.
loop--;
continue;
} else {
// This is a unique number so add it to the sequence.
sequence[loop] = number;
}
}
} else {
// Repetitive values are allowed.
for (NSUInteger loop = 0; loop < length; loop++) {
// Only unique values are allowed.
if (![JFRandom generateNumberBetweenLow: low
andHigh: high
intoReceiver: &number]) {
return NO;
}
sequence[loop] = number;
}
}
return YES;
}
/*
* Randomly chooses a number from the provided sequence and places it into the receiver.
*
* Params
* sequence The receiving sequence.
* Must not be nil and must be of at least 'length' numbers.
* length The length of the sequence.
* Must be at least 1.
* receiver The receiver for the random number.
* Must not be nil.
*
* Return
* YES if successful, NO otherwise.
*/
+ (BOOL) chooseNumberFromSequence: (NSInteger[]) sequence ofLength: (NSUInteger) length intoReceiver: (NSInteger *) receiver {
if (length < 1) {
return NO;
}
if (sequence == nil) {
return NO;
}
if (receiver == nil) {
return NO;
}
NSInteger number;
// Generate a random index into the sequence...
if (![JFRandom generateNumberBetweenLow: 0
andHigh: length - 1
intoReceiver: &number]) {
return NO;
}
*receiver = sequence[number];
return YES;
}
/*
* Returns YES if the provided number appears within the sequence.
*
* Params
* number The number to test.
* sequence The sequence within which to test.
* Must not be nil and must be of at least 'length' numbers.
* length The length of the sequence.
* Must be at least 1.
*
* Return
* YES if the number was found, NO otherwise.
*/
+ (BOOL) isNumber: (NSInteger) number inSequence: (NSInteger[]) sequence ofLength: (NSUInteger) length {
if (length < 1) {
return NO;
}
if (sequence == nil) {
return NO;
}
for (NSUInteger check = 0; check < length; check++) {
if (sequence[check] == number) {
// The number was found so return YES.
return YES;
}
}
// The number was not found.
return NO;
}
/*
* Returns an NSData populated with bytes whose values range from 0 to 255.
*
* Params
* length The length of the resulting NSData.
* Must be 1 or greater or nil will be returned.
*
* Returns
* An NSData instance containing random bytes.
*/
+ (NSData *) generateRandomDataOfLength: (NSUInteger) length {
if (length == 0) {
return nil;
}
NSInteger *sequence = malloc(sizeof(NSInteger) * length);
[JFRandom generateNumberSequenceOfLength: length
into: sequence
betweenLow: 0
andHigh: 255
withOnlyUniqueValues: NO];
char *randData = malloc(length);
for (NSUInteger loop = 0; loop < length; loop++) {
randData[loop] = (char) sequence[loop];
}
free(sequence);
NSData *data = [NSData dataWithBytes: randData
length: length];
free(randData);
return data;
}
/*
* Returns an NSData populated with bytes whose values range from -128 to 127.
*
* Params
* length The length of the resulting NSData.
* Must be 1 or greater or nil will be returned.
*
* Returns
* An NSData instance containing random signed bytes.
*/
+ (NSData *) generateRandomSignedDataOfLength: (NSUInteger) length {
if (length == 0) {
return nil;
}
NSInteger *sequence = malloc(sizeof(NSInteger) * length);
[JFRandom generateNumberSequenceOfLength: length
into: sequence
betweenLow: -128
andHigh: 127
withOnlyUniqueValues: NO];
signed char *randData = malloc(length);
for (NSUInteger loop = 0; loop < length; loop++) {
randData[loop] = (signed char) sequence[loop];
}
free(sequence);
NSData *data = [NSData dataWithBytes: randData
length: length];
free(randData);
return data;
}
/*
*
* Returns a string populated with random characters.
*
* Params
* length The length of the resulting string.
* Must be 1 or greater or nil will be returned.
*
* Returns
* An ASCII encoded NSString instance.
*/
+ (NSString *) generateRandomStringOfLength: (NSUInteger) length {
if (length == 0) {
return nil;
}
NSInteger *sequence = malloc(sizeof(NSInteger) * length);
[JFRandom generateNumberSequenceOfLength: length
into: sequence
betweenLow: 0
andHigh: 255
withOnlyUniqueValues: NO];
char *randData = malloc(length);
for (NSUInteger loop = 0; loop < length; loop++) {
randData[loop] = (char) sequence[loop];
}
free(sequence);
NSString *string = [NSString stringWithCString: randData
encoding: NSASCIIStringEncoding];
free(randData);
return string;
}
+ (NSString *) generateRandomTypableStringOfLength: (NSUInteger) length
{
if (length == 0) {
return nil;
}
NSInteger *sequence = malloc(sizeof(NSInteger) * length);
[JFRandom generateNumberSequenceOfLength: length
into: sequence
betweenLow: 33
andHigh: 125
withOnlyUniqueValues: NO];
char *randData = malloc(length);
for (NSUInteger loop = 0; loop < length; loop++) {
randData[loop] = (char) sequence[loop];
}
free(sequence);
NSString *string = [NSString stringWithCString: randData
encoding: NSASCIIStringEncoding];
free(randData);
return string;
}
@end