The random function, generates the same values each the time program is run.. Any other function to output random numbers, each time the program is run ??

Recommended Answers

All 18 Replies

The usual method is seeding rand() with the current time:

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

int main(void)
{
    int i;

    srand((unsigned)time(NULL));

    for (i = 0; i < 10; i++)
        printf("%3d", rand() % 100);

    putchar('\n');

    return 0;
}

OK thnx..
Is there restrictions to the random function, i mean if a number has been output it will not be output again ?

Lets say we need to print 6 numbers, a number can appear only once.

no there are no such restrictions

but if i need to apply that in my work, how can i do it ?? any idea

but if i need to apply that in my work, how can i do it ??

Generate the numbers, then shuffle them randomly. For example, see this thread, or this thread.

however if all the numbers are not in the requirements random shuffle wont work.
If the above threads dont solve your problem,you would need to go for finding duplicates and there are several algorithms to do that.All depends on the application of the code,as complexity of the algorithms makes them specific for different purposes

however if all the numbers are not in the requirements random shuffle wont work.

Your use of the word "requirements" is confusing. Can you elaborate?

Generate the numbers, then shuffle them randomly. For example, see this thread, or this thread.

Yea thnx mate.. the post surely helped..

Your use of the word "requirements" is confusing. Can you elaborate?

What I mean is that suppose you want 6 nos from a range of 1-1000,random shuffle would have to be modified.

What I mean is that suppose you want 6 nos from a range of 1-1000,random shuffle would have to be modified.

There's nothing about that use case that would require random shuffle to be modified. Taking a sample of 6 numbers from a randomly shuffled array of 1000 is trivial even on memory constrained systems; there's no requirement that you must use all of the numbers from the sequence.

It's also a use case where searching for duplicates in the result set is likely to be much faster than a random shuffle due to the small size of the result set and size of the result set relative to the range. In other words, you're correct that checking for duplicates and re-generating the random number if necessary is probably the better option given an assumption that the range is large and the sample set is small:

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

#define SET_SIZE 6
#define LOWER_BOUND 1
#define UPPER_BOUND 1000

static bool exists(int a[], size_t n, int value)
{
    for (int i = 0; i < n; i++) {
        if (a[i] == value)
            return true;
    }
    
    return false;
}

int main(void)
{
    srand((unsigned)time(NULL));
    
    int set[SET_SIZE];
    int n = 0;
    
    while (n < SET_SIZE) {
        int r = LOWER_BOUND + rand() % UPPER_BOUND;
        
        if (!exists(set, n, r))
            set[n++] = r;
    }
    
    for (int i = 0; i < n; i++)
        printf("%4d", set[i]);
        
    puts("");
}

@narue i got a question for ya..

suppose i got two array of integers which i used scanf to get the values..
how can i compare the array of values?

the first one::

for( i=0;i<6;i++)

        scanf("%d",&num[i]);

the second one::

for ( i = 0; i < N; i++ )
        seq[i] = i;
         
        /* Random shuffle */
        for ( i = 0; i < N - 1; i++ ) 
        {
            int r = ( rand() % ( N - i ) + 1 );
            int save = seq[i];
            seq[i] = seq[i + r];
            seq[i + r] = save;
        }

how can i compare the array of values?

What do you mean by compare them? Are you trying to match the two arrays in both values and order? Just values? What exactly are you looking for in the comparison?

What do you mean by compare them? Are you trying to match the two arrays in both values and order? Just values? What exactly are you looking for in the comparison?

Lets say for a Lotto game, in the first array are the numbers the user input, and the other one are those randomly output.. How can i compare the two array to know how many numbers are the same ??

That's a comparison for matching values in any order. You can use nested loops since the arrays are small:

int matches = 0;

for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        if (seq[i] == num[j]) {
            ++matches;
            break; // Don't count duplicates in num
        }
    }
}

There's an implicit assumption that there are no duplicates in seq as it's generated using a random shuffle. The values of num are coming in from user input, so I added a little insurance to account for accidental duplicates. It also has the benefit of breaking the inner loop early and thus being slightly faster, but that's not a big win for small arrays.

random function in C having some restrictions..........

random function in C having some restrictions..........

Fascinating. Care to elaborate? :icon_rolleyes:

Hey mate suppose i have the following codes for inserting numbers in an array

printf("\n  Enter the 6 numbers ( 1 - 40): ");
   
  
   for( i=0;i<6;i++)                    

        scanf("%d",&num[i]);

For each number it has to be inserted on the next line till 1=6, is there a way for the numbers to be inserted on a single line..

for this method on each line its starting at the left hand side, i tried to use the \t code for it to be a tab away from the left, it does not work, what can be done for that ?
But i prefer if its possible for it to be on a single line !
thanks

For each number it has to be inserted on the next line till 1=6

That's an assumption on your part. Nothing in the code requires you to hit the Enter key between numbers, just separate them by whitespace on the same line and scanf() will do the right thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.