Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mahaboob

macrumors member
Original poster
Jul 10, 2008
31
0
I need to produce 15 random numbers between 1 to 16 without repeating any number. I used the code like

Code:
    int i,j;    
    for(i=0;i<15;i++){    
        j =random() % 15 +1;
        NSLog(@"No: %d => %d \n",i,j);
        srandom(time(NULL)+i);
    }

But some numbers are repeating.
How can I do it without repeating the numbers?

Thanks in advance
Mahaboob
 

gekko

macrumors 6502
Mar 20, 2009
271
0
You should only call srandom once and before calls to random, like so:
Code:
    int i,j;    
    srandom(time(NULL));
    for(i=0;i<15;i++){    
        j =random() % 15 +1;
        NSLog(@"No: %d => %d \n",i,j);
    }
srandom initialises the random number generator.

Sorry, I read it too fast. You can't produce random numbers that don't repeat using this method. You have to select the numbers and keep track of which ones have already been picked in some way.
 

zacheryjensen

macrumors 6502a
May 11, 2009
801
187
I need to produce 15 random numbers between 1 to 16 without repeating any number. I used the code like

Code:
    int i,j;    
    for(i=0;i<15;i++){    
        j =random() % 15 +1;
        NSLog(@"No: %d => %d \n",i,j);
        srandom(time(NULL)+i);
    }

But some numbers are repeating.
How can I do it without repeating the numbers?

Thanks in advance
Mahaboob

Isn't this somewhat off topic?

Anyway, what you want is not 15 random numbers. What you want is the numbers 1-16 in a random order. So you would have an array or similar with all 15 numbers in it, then shuffle their order. There are a variety of shuffling algorithms but you could start with something simple. Fill a 15 length array with 0-15. Loop over each element in the array, swap its value with another randomly selected index of the array.

Then google around for some more effective shuffling algorithms with less likelihood of sequence and grouping.
 

colmaclean

macrumors 68000
Jan 6, 2004
1,702
348
Berlin
A number of ways to do that.

You could store the list of numbers already generated and compare each newly-generated number against this list to ensure it's not already there. If it is, generate another number, if it isn't add it to the list.

Or you could set up an array of 15 elements. Each element could represent a number from 1-15. Each time you generate a new number, the corresponding element is checked off. If you generate a number which is already checked off, move to the next available element and check it off.

And so on...
 

animefx

macrumors regular
May 10, 2005
157
0
Illinois
i think you could check for the number generated and somehow mark it off your list in memory. one thing you should be using is the != (not equal to) operator.

so maybe each time it goes through the loop it makes sure the result of the random number is != to one of the previous random numbers that has chosen already.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
You need to get a book on computer algorithms. The common way to do this is to create a list of numbers, and shuffle the list randomly, then you can read the list in any order and never see a repeat.
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
I need to produce 15 random numbers between 1 to 16 without repeating any number. I used the code like

Code:
    int i,j;    
    for(i=0;i<15;i++){    
        j =random() % 15 +1;
        NSLog(@"No: %d => %d \n",i,j);
        srandom(time(NULL)+i);
    }

But some numbers are repeating.
How can I do it without repeating the numbers?

Thanks in advance
Mahaboob

Code:
	int i, j, t, array[16];

	for ( i = 0; i < 15; i++ ) {
		array[i] = i;
	}

	srandom( time(NULL) );
	for ( i = 0; i < 15; i++ ) {
		j = random() % 15;

		t = array[i];
		array[i] = array[j];
		array[j] = t;
	}
	
	for ( i = 0; i < 15; i++ ) {
		NSLog(@"No: %d => %d \n", i, array[i]);
	}

If you don't mind mixing in some C++

Code:
#import <Cocoa/Cocoa.h>

#include <algorithm>
#include <vector>

int main(int argc, char* argv[])
{
	// allocate vector of 16 int's named "array"
	std::vector<int>	array(16);

	// init vector "array" to contain the sequence: 0 - 15
	for ( std::vector<int>::iterator itr = array.begin(); itr != array.end(); itr++ )
	{
		*itr = (itr - array.begin());
	}

	// shuffle (random order) the vector "array"
	std::random_shuffle(array.begin(), array.end());

	// output the now shuffled sequence of the vector "array"
	for ( std::vector<int>::iterator itr = array.begin(); itr != array.end(); itr++ )
	{
		NSLog(@"No: %d => %d \n", (itr - array.begin()), *itr);
	}
}
 

Kingbombs

macrumors member
Jun 24, 2009
98
0
have a look at arc4random()
You may find it a bit better without having to seed the time
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Out of curiosity, why do you think arc4random() is better than random() for generating a list of 15 numbers in random order?
 

Kingbombs

macrumors member
Jun 24, 2009
98
0
infact, why wouldn't you use arc4random() ?
From what i can find out, its not really any slower, its more precision and can be used to creating floating point numbers
so why use anything else but arc4random for generating random numbers
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.