2

I was making a puzzle game and i want to generate a list of random numbers between some limit. I have already used rand and srand function but it gives me duplicate value also. I want to generate a random list without duplication how would i do that?

Mayank
  • 21
  • 1
  • 3
  • Are you saying it's generating the same numbers on every call? Or just that occasionally it repeats numbers? If the latter, I know of no better way than keeping track of the numbers you've already generated and maintaining that information as you generate unique values. – prelic Jan 19 '12 at 17:23
  • 3
    http://stackoverflow.com/a/196065/857994 actually is better :) – John Humphreys Jan 19 '12 at 17:24
  • @w00te: This is not exactly the same question as here an small upper limit is not specified for the random numbers. – salva Jan 19 '12 at 18:53
  • Yeah, I realized that but I thought it was helpful. Notice I didn't cast a close vote :) – John Humphreys Jan 19 '12 at 18:58

5 Answers5

9

The usual approach for this is something like:

  populate an array source_array of size <n> with numbers from 0 to n-1

  while n > 0
  use rand to generate a random number x in the range 0..n-1

  add source_array[x] to the result list

  source_array[x] = source_array[n-1]; // replace number just used with last value

  --n; // next time, one less number
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • and see http://stackoverflow.com/questions/196017/unique-random-numbers-in-o1/196065#196065 for a more detailed explanation, with pictures – David Gelhar Jan 19 '12 at 17:29
0

I'll presume you are using an array to store the numbers, you could use something like this inArray() function and use it with a do ... while loop which would generate a random number until it generates one that is not in the array

Edit: w00te's comment links to this answer which I believe is better then my method and definitely worth a read. This is also what David Gelhar is briefly suggesting.

T I
  • 9,785
  • 4
  • 29
  • 51
0

This method results appropriate when the limit is high and you only want to generate a few random numbers.

#!/usr/bin/perl

($top, $n) = @ARGV; # generate $n integer numbers in [0, $top)

$last = -1;
for $i (0 .. $n-1) {
    $range = $top - $n + $i - $last;
    $r = 1 - rand(1.0)**(1 / ($n - $i));
    $last += int($r * $range + 1);
    print "$last ($r)\n";
}

Note that the numbers are generated in ascending order, but you can shuffle then afterwards.

salva
  • 9,943
  • 4
  • 29
  • 57
-1

To avoid duplication, you could store all generated numbers in a list and on each iteration, check if the number was already generated; If yes, regenerate, if not add it to the list and show it.

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
-1

You can do this:

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

#define MAX_NUMBER_VALUE 60
#define HOW_MANY_NUMBERS 10

int main ( void )
{
    int numbers[HOW_MANY_NUMBERS];
    int counter = 0;
    while ( counter < HOW_MANY_NUMBERS)
    {
        int tempRandom = rand ( MAX_NUMBER_VALUE + 1 );
        int check = 0;
        while ( check <= counter )
        {
            if ( number[counter] == number[check] )
                tempRandom = rand ( MAX_NUMBER_VALUE + 1 );
        }
        printf("Random number #%d - Value: %d", counter, tempRandom);
    }
}

You need perform this, but it works.

Bruno Alano
  • 643
  • 1
  • 11
  • 21