unique randomly generated numbers

This is a discussion on unique randomly generated numbers within the C++ Programming forums, part of the General Programming Boards category; i am currently working on a program that generates 50 sets of six randomly generated lottery numbers, using a two ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Question unique randomly generated numbers

    i am currently working on a program that generates 50 sets of six randomly generated lottery numbers, using a two dimensional array. i have been able to generate and sort these numbers in ascending order. however, my method for testing each row of the array for unique numbers is not working 100%. i have included the code as follows from a much earlier assignment. if someone could give me a clue as to how to test just 6 ramdomly generated numbers for uniqueness, i'me sure i can take it from there. even a simple algorithm might be all i need.


    //T5Be01
    //this program generates six random numbers
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <fstream.h>

    void main()
    {
    short num1 = 0;
    short num2 = 0;
    short num3 = 0;
    short num4 = 0;
    short num5 = 0;
    short num6 = 0;

    srand (time(NULL));
    num1 = 1 + rand() % (54 - 1 + 1);
    num2 = 1 + rand() % (54 - 1 + 1);
    num3 = 1 + rand() % (54 - 1 + 1);
    num4 = 1 + rand() % (54 - 1 + 1);
    num5 = 1 + rand() % (54 - 1 + 1);
    num6 = 1 + rand() % (54 - 1 + 1);

    cout<< "Num 1 = "<< num1<< endl;
    cout<< "Num 2 = "<< num2<< endl;
    cout<< "Num 3 = "<< num3<< endl;
    cout<< "Num 4 = "<< num4<< endl;
    cout<< "Num 5 = "<< num5<< endl;
    cout<< "Num 6 = "<< num6<< endl;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you will find this easier to do if you ditch the array and use a binary search tree.Cprogramming.com has a tutorial... go have a read and come back if you dont understand.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 08:16 AM
  3. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21