Unique random numbers

This is a discussion on Unique random numbers within the C Programming forums, part of the General Programming Boards category; ok i read the FAQ on random numbers but couldnt make much sense of it, i am writing code for ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Unique random numbers

    ok i read the FAQ on random numbers but couldnt make much sense of it, i am writing code for an assigments, which is to create a lottery program, here is the code i have so far:
    Code:
     
    #include "stdafx.h"
    #include "time.h"
    int lottery[6];
    
    void main()
    {
    	srand((unsigned)time(NULL));
    	lottery[0] = rand()%49;
    	lottery[1] = rand()%49;
    	lottery[2] = rand()%49;
    	lottery[3] = rand()%49;
    	lottery[4] = rand()%49;
    	lottery[5] = rand()%49;
    	printf("%d\t%d\t%d\t%d\t%d\t%d\n\n", lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], lottery[5]);
    	system("pause");
    
    }
    can anyone help so i can create UNIQUE random numbers?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    1. Create the first random number, store it in array.
    2. Create next random number.
    3. Check if number generated exists in array, if not, add to array.
    4. If array full, move on, otherwise go back to step 2.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    how do you check if the number is the same in the array

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    You're probably going to need to use nested loops. An outer loop which keeps generating numbers until we have inserted X number of unique values, and an inner loop which checks the array from index 0 up to (not including) whatever the current count is at.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > void main()
    int main

    Perhaps you should consider say
    int array[49];
    store 1 to 49 in each respective slot

    shuffle the array - randomly exchange pairs of array elements
    exchanging disturbs the order, but preserves the uniqueness

    take the first six elements of the array as your lottery draw

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by aydin
    how do you check if the number is the same in the array
    i think that we are ending up confusing the guy,he probably is just starting out with C.i think that what hk_mp5kpdw said is nice enough.try to implement it on your own

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int is_number_in_array(int lottery[],int size, int number){
        //cicle the array and compare the number.
        // return 1 when match is found (1 means true)
        return 0;
    }
    
    int main(){
        int lottery[6];
    
        srand((unsigned)time(NULL));
        /*************
        LOOP1: loop till 6 number have been generated
           LOOP2:
                generate random number
                check if it is in array//use a function
                if so continue loop2
            place number in array        
        ******************/
    
        printf("%d\t%d\t%d\t%d\t%d\t%d\n\n", lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], lottery[5]);
        system("pause");
        return 0;
    }
    Now the part you wanted (to check if a number is part of the array) you write it.
    Last edited by xErath; 11-23-2004 at 11:54 AM.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Salem's answer is probably the easist and most effective for this problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM

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