Random Number Generator in Python and Objective-C

If you want a simple number generator for say picking Keno, lotto, or self made contests, here’s just how simple it is to do in Python. I was listening to Linux Outlaws awhile back and they were using a couple of different scripts to generate winners for a contest so why not write one real quick?

#! /usr/bin/python
'''
C. Nichols
'''
import random

def slot_machine(slot_range=(1,100),slots=6):
    """slot_machine(slot_range=(1,100),slots=6) -> tuple(int(slot),int(pick))"""
    min,max=slot_range
    pool=[num for num in range(min,max+1)]
    for slot in range(1,slots+1):
       yield slot,random.choice(pool)   

# ============================================================================
# Main: #    Run two common draws as an example.
# ============================================================================
print 'Typical Kino, with slot number.'
for n in slot_machine(slot_range=(1,80),slots=20):
    print n  

print '\nOhio "Classic Lotto", showing just the pick.'
for n in slot_machine(slot_range=(1,49),slots=6):
    print n[-1]

Here’s a another version. Also, I have a iPhone example that will not repeat numbers that you can download here.

#! /usr/bin/python

"""
Author: C. Nichols 
Site: http://www.darkartistry.com

Simple randomizer function.
"""
import random

def random_set(limit_picks=3, choices=[1,2,3,4,5,6,7,8], last_picks=[1,5,8]):
    """Picks random items in a list returning a list of the amount you want
    and filtering the last set of picks."""
    item_count = set()

    random.shuffle(choices)

    for random_item in choices:
        # see if it's already been picked.
        if random_item in last_picks: continue
        # get a count.
        if len(item_count) == limit_picks: break
        item_count.add(random_item)
        # return the result.
        yield random_item

# MAIN: get and print random items from a list...
results = [i for i in random_set()]
print results

 

I am learning Objective-C so I decided to create a similar one in XCode. Maybe it can be useful to someone.

//  main.m
//  NumGenie
//
//  Created by Charles Nichols.

#import 
#include 

NSDictionary * slot_machine (int minRange, int maxRange, int slotCount)
{
    NSMutableDictionary *myNumbers = [NSMutableDictionary dictionary];
    do
    {
        int number = (arc4random() % (maxRange - minRange)) + minRange;
        [myNumbers setObject:[NSNumber numberWithInt:number]
                                          forKey:[NSNumber numberWithInt:slotCount]];
        slotCount--;
    } while (slotCount > 0);
    return myNumbers;
}

// Main
int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Here's the Keno example in Objective-C
    NSDictionary *myDraw = [NSMutableDictionary dictionary];
    myDraw = slot_machine(1,80,20);

    // Sort our dictionary and print.
    NSArray *sortedKeys = [[myDraw allKeys]
                                  sortedArrayUsingSelector: @selector(compare:)];
    for (NSString *key in sortedKeys)
        NSLog(@"slot=%@ value=%@", key, [myDraw objectForKey:key]);

    [pool drain];
    return 0;
}

~ by mohawke on May 28, 2012.