INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

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.

Jobs

two quick questions

two quick questions

(OP)
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?  

cheers,

craig.

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.

CODE

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

#define NOTPICKEDMAX 50
#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
        remain--;
        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!


Resources


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