Sorting Random Numbers

This is a discussion on Sorting Random Numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, 1st time poster. I'm currently working on a program that generates random numbers 1-49. I want to sort them ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Question Sorting Random Numbers

    Hi, 1st time poster.
    I'm currently working on a program that generates random numbers 1-49. I want to sort them from lowest to highest, how would i come about doing that?
    here's what i got so far
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> //for the seedrnd() function 
    
    
    #define RANGE 49 //number of numbers 
    #define BALLS 7 //number of balls to draw 
    #define DELAY 1000000 //delay interval between picks 
    
    
    int rnd(int range); 
    char loop;
    void seedrnd(void); 
    
    
    void main() 
    { 
     do { 
        int numbers[RANGE]; //array that holds the balls 
        int i,b; 
        unsigned long d; //delay variable 
        seedrnd(); //seed the randomizer 
    /* initialize the array */ 
    
        for(i=0;i<RANGE;i++) //initialize the array 
            numbers[i]=0; 
       
        printf("Press Enter to pick this week's numbers:"); 
        getchar(); 
    
    /* draw the numbers */ 
        for(i=0;i<BALLS;i++) 
        { 
            for(d=0;d<=DELAY;d++); //pause here 
    /* picks a random number and check to see whether it's already been picked */ 
    
            do 
            {  
                b=rnd(RANGE); //draw number 
            } 
            while(numbers[b]); //already drawn? 
    
            numbers[b]=1; //mark it as drawn   
            printf(" %i ",b+1); //add one for zero 
        }
        printf("\n Press 1 to return");
        scanf("%d", &loop);
    }
    while (loop==1);
    
    } 
    
    
    /* Generate a random value */ 
    
    int rnd(int range) 
    { 
        int r; 
        r=rand()%range; //spit up random number 
        return(r); 
    } 
    
    /* Seed the randomizer */ 
    
    void seedrnd(void) 
    
    { 
        srand((unsigned)time(NULL)); 
    }
    Thanks in advance!

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Ok...First of all welcome, anything I can do to help you on these boards please let me know. Second of all: Thank you very much for reading the Stickies and announcements heh. I really appreciate that.

    Now, just an FYI, I edited your code slighty, I just made it more readable. What it looks like you need now is sort it. There are several diffrent sorting algorithms you can use. I would use google to take a look at some of them, or your text book. The easiet one to use is called bubble sort or insertion sort.

    I hope this helps you start. Good luck, feel free to ask if you have anymore problems.


    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Since you are only sorting a small amount of numbers, I'd suggest looking for information on bubble sort. Its the easiest of all the sorts out there to implement (although also probably the slowest, but that won't matter here). It basically goes like this:
    Code:
    for (int x=0; x<arraysize; x++)
      for (int y=x+1; y<arraysize; y++)
          if (array[x]>array[y]) then swap the two

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    here's an insertion sort

    Code:
    template<class T>
    void insertionSort(std::list<T>& listRef)
    {
      list<T>::iterator iter1, iter2, iter3;
      iter1 = listRef.end();
      
      for(iter2 = listRef.begin(); iter2 != iter1; iter2 = listRef.erase (iter2)) {
        iter3 = listRef.begin();
        while(iter3 != listRef.end() && *iter2 > *iter3)
          iter3++;
        listRef.insert(iter3, *iter2);
      }  
    
    }
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  5. #5
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    void main() bad.
    int main() good.


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, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. Random numbers
    By ActionMan in forum C Programming
    Replies: 6
    Last Post: 03-15-2002, 09:26 AM

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