0

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 ??

4
Contributors
18
Replies
20
Views
5 Years
Discussion Span
Last Post by Narue
Featured Replies
  • 1
    Narue 5,707   5 Years Ago

    [QUOTE]but if i need to apply that in my work, how can i do it ??[/QUOTE] Generate the numbers, then shuffle them randomly. For example, see [URL="http://www.daniweb.com/software-development/c/threads/346402"]this thread[/URL], or [URL="http://www.daniweb.com/software-development/c/threads/27314"]this thread[/URL]. Read More

0

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;
}
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.

0

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

0

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?

0

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.

0

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("");
}
0

@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;
        }

Edited by Master Mascus: n/a

0

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?

0

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 ??

0

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.

0

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

Fascinating. Care to elaborate? :icon_rolleyes:

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.