picking six unique random numbers

I'm toying around with a program in C++ that picks a user defined amount of number sets (ie: 6 numbers per set, and the user may want 100 sets) and then gives me a count of how many times each number was picked (right now its 1 to 49).

My problem is that when I am picking a set of 6 numbers I am wracking my brains to find a way to make it pick only 6 unique numbers...

Right now I have 2 loops and somehow it doesn't seem to be elegant.

do loop until 6 numbers are actually picked - get a random number (I'm using RAND right now) - start another loop (6 times) - check latest pick against the numbers picked before (stored in a temporary array) - end loop, if number wasn't found, then add to set and increment the numbers that were pickedend loop

I'm just curious if someone might have a nicer way to do it, I can post code if needed, but I'm interested in the algorithm since I keep playing with this thing as a way to practice coding C++ (I learned to program in C and C++ but been working COBOL and mainframes for the last 5 years so I never did anything with it beyond the basics I was taught in college and I want to change that).

Thanks in advance for your help, I've been reading the boards for weeks but its my first question...this place totally rocks.

Comments

: : do loop until 6 numbers are actually picked: - get a random number (I'm using RAND right now): - start another loop (6 times): - check latest pick against the numbers picked before : (stored in a temporary array): - end loop, if number wasn't found, then add to set and : increment the numbers that were picked: end loop: : Looks ok to me. BTW, there's no such thing as a random number, only random sequences.

: Looks ok to me. BTW, there's no such thing as a random number, only random sequences.: :

Well ok then, random sequences if you want to get picky

I'm still toying with it, and I'm happy to get a second opinion that says my solution looks ok (thank you for your reply) but you know...ever get that feeling when you look at a piece of code that it could be done better, but you can't figure out how?

: : I'm still toying with it, and I'm happy to get a second opinion that says my solution looks ok (thank you for your reply) but you know...ever get that feeling when you look at a piece of code that it could be done better, but you can't figure out how?:

: : : : I'm still toying with it, and I'm happy to get a second opinion that says my solution looks ok (thank you for your reply) but you know...ever get that feeling when you look at a piece of code that it could be done better, but you can't figure out how?: : : : Frequently. 8-): :

[italic]: I'm just curious if someone might have a nicer way to do it, I can post code if needed, but I'm interested in the algorithm since I keep playing with this thing as a way to practice coding C++ (I learned to program in C and C++ but been working COBOL and mainframes for the last 5 years so I never did anything with it beyond the basics I was taught in college and I want to change that).[/italic]

Well yeah, that's how it started. I mean it means nothing to me that they are 6/49 lottery picks. It started as an argument between me and a friend over coding practice.. For some reason he described to me this program that he wanted to play with and couldn't get his mind around storing the total times each number (from 1 to 49) had been picked into an arrray of 49 ints. So I coded the barebones of it to show him and then I started toying with it more and more. It was just the thing to get my kickstart back into C++ coding (currently I work with mainframes and its boring as h*ll).

[color=000080]int[/color] main() { [color=80a0b0][italic]// get fifteen random numbers in range [0-49][/italic][/color] list<[color=000080]int[/color]> nums = unique_rand ([color=bb0000]15[/color], [color=bb0000]50[/color]); copy (nums.begin(), nums.end(), ostream_iterator<[color=000080]int[/color]>(cout, [color=bb0000]"[/color][color=907050][/color][color=bb0000]"[/color])); [color=000000][b]return[/b][/color] [color=bb0000]0[/color]; }[/code]You can easily modify it to return the list of numbers some other way that returning list (such as passing in an appropriately sized int array). You could also modify it to select random numbers in a range (min-max) rather than the current (0-max). I'll leave that as an exercise for you.

Cheers,Eric

P.S. Here's the same algorithm in Perl, Lua and Java (because I'm bored):

: Here's an algorithm that will give you n unique random numbers on O:(n) time (i.e. - one pass):

Thanks! More code to play with

: You can easily modify it to return the list of numbers some other way :that returning list (such as passing in an appropriately sized :int array). You could also modify it to select random numbers in a :range (min-max) rather than the current (0-max). I'll leave that as :an exercise for you.

I very much appreciate it actually This whole thing sounds pretty dinky, but it got me back into C++ programming and MFC app development so I'm having a lot of fun tinkering.

[italic]: I notice that the condition (I think) basically works in that you pick the first random number...then you keep making sure each pick is higher than the last...which means there are no duplicates.[/italic]

True.

[italic]: However, when i=0, what happens if Rand() returns the highest number possible?: : I tried this and the program just gets stuck. (I forced in 49, I haven't played with it in debug yet).[/italic]

Also true. I wrote this from scratch and didn't implement things like "error checking".

Lua rocks. It's a tiny, functional language (functions are first class object). It's easy to learn, easy to read, and is very powerful, but best of all: it is designed to be embedded in other applications. You can extend the language from C or C++ code and use it as scripting language for your app. It's tiny (will add about 50Kb to your app), and is super fast (faster than almost all other interpreted scripting languages, including Perl, Python, Tcl, etc.).

: I'm toying around with a program in C++ that picks a user defined amount of number sets (ie: 6 numbers per set, and the user may want 100 sets) and then gives me a count of how many times each number was picked (right now its 1 to 49).: : My problem is that when I am picking a set of 6 numbers I am wracking my brains to find a way to make it pick only 6 unique numbers...: : Right now I have 2 loops and somehow it doesn't seem to be elegant.: : do loop until 6 numbers are actually picked: - get a random number (I'm using RAND right now): - start another loop (6 times): - check latest pick against the numbers picked before : (stored in a temporary array): - end loop, if number wasn't found, then add to set and : increment the numbers that were picked: end loop: : I'm just curious if someone might have a nicer way to do it, I can post code if needed, but I'm interested in the algorithm since I keep playing with this thing as a way to practice coding C++ (I learned to program in C and C++ but been working COBOL and mainframes for the last 5 years so I never did anything with it beyond the basics I was taught in college and I want to change that).: : Thanks in advance for your help, I've been reading the boards for weeks but its my first question...this place totally rocks.:

here is my own algorithm in a pseudo-language(sorry) to pick 6 unique numbers in a set of numbers between 1 to 49 for an exemple.

: I'm toying around with a program in C++ that picks a user defined amount of number sets (ie: 6 numbers per set, and the user may want 100 sets) and then gives me a count of how many times each number was picked (right now its 1 to 49).: : My problem is that when I am picking a set of 6 numbers I am wracking my brains to find a way to make it pick only 6 unique numbers...: : Right now I have 2 loops and somehow it doesn't seem to be elegant.: : do loop until 6 numbers are actually picked: - get a random number (I'm using RAND right now): - start another loop (6 times): - check latest pick against the numbers picked before : (stored in a temporary array): - end loop, if number wasn't found, then add to set and : increment the numbers that were picked: end loop: : I'm just curious if someone might have a nicer way to do it, I:can post code if needed, but I'm interested in the algorithm since I :keep playing with this thing as a way to practice coding C++ (I :learned to program in C and C++ but been working COBOL and mainframes:for the last 5 years so I never did anything with it beyond the basics :I was taught in college and I want to change that).: : Thanks in advance for your help, I've been reading the boards for :weeks but its my first question...this place totally rocks.:

Hello!primarily, its totally no matter which algo you are using for your6 out of 49 lottery program, as long as it WORKS properly, without duplicates and "number holes" or repetitious series of numbers(in BASIC i would prevent this by the RANDOMIZE TIMER statement, and its ok)so just write YOUR code!

But btw, could you tell me where to get a good C/C++ Interpreter/Compiler/Development environment from?I would like to start programming in C/C++/VisualC++ too, after BASIC,VisualBASIC and a little Pascal and ASM(with Microsofts DEBUG!! ;-) ).What Compiler/Environment are you using?