Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

two quick questions

two quick questions

two quick questions

firstly, im trying to generate 'random' numbers between zero and 49 (guess what for).  currently im using:

time_t t;
srand(unsigned (time(&t)));
int num = (rand() % 49);

but as that stands, the numbers im getting dont seem very random. in fact they seem like theyre just being plucked from a sequential count through 0 - 49.  also, its wasting a lot of time in my while loop becuase the same number will be generated lots of times before it changes.  what more effective way is there of achieving this?

also, i have a simple array which im putting these numbers into "numbers[6]".  for my 6 numbers.  what sorta syntax would i need to use to sort it into ascending order using the qsort() function?  or failing that, what other simple sort algorithms are there that could be used?  



RE: two quick questions

answer to ur second question:

why not use a simple sorting alg like bubble sort or selection sort and get done with it, and ur array size seems to b really small and no optimization is needed.

RE: two quick questions

The algo below is what I use for picking unique cards in the card games I used to write in Uni.  Basically pick one and put the one at the end in its position.
You could use selection sort to sort the numbers.


#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define PICKEDMAX 6
int main(int argc, char* argv[])
    int notpicked[NOTPICKEDMAX], n, p, remain, picked[PICKEDMAX], temp;
    srand (time (0));
    // Set up a list of numbers we can pick
    for (n = 1; n < NOTPICKEDMAX; n++)
        notpicked[n] = n;
    remain = NOTPICKEDMAX;
    for (p = 0; p < PICKEDMAX; p++)
        // Get a number
        n = rand () % remain + 1;
        picked[p] = notpicked[n];
        // Move the one that we did not choose to the vacant position
        notpicked[n] = notpicked[remain];
    // Sort the numbers in order
    for (p = 0; p < PICKEDMAX - 1; p++)
        for (n = p + 1; n < PICKEDMAX; n++)
            if (picked[p] > picked[n])
                temp = picked[n];
                picked[n] = picked[p];
                picked[p] = temp;
    // Print it out
    for (p = 0; p < PICKEDMAX; p++)
        printf ("%d ", picked[p]);
    printf ("\n");
    return 0;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close