C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 12th, 2003, 03:50 PM
kavi_s kavi_s is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 24 kavi_s User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
rand() to generate ARRAY of numbers

Hi all,

I need to generate an array of random integers. The number of integers can vary depending on some formula. The most important thing is that the integers MUST be unique. I can't have duplicate numbers occurring throughout the array. How can I go about this? At the moment, I just have a for loop that iterates as many times are there is space in the array, and I call the "rand" function each time, like:

Code:
  array[i] = (int)( ( ( (double)rand() )/RAND_MAX)*10); 
  //I want the range to be from 0 to 10
  //i is the for loop variable


Obviously, this does not take care of UNIQUENESS.
Is there any function that can generate an ARRAY instead which contains UNIQUE integers???

THanks!
Kavi

Reply With Quote
  #2  
Old June 12th, 2003, 04:13 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
one way would be to use two arrays. you say the random number is going to be between 1 and 10? so that's a maximum of 9 possible unique number then.

build two arrays, one with the number of elements in your required range which is 9 in your case i think. fill the array using a for loop in a sequential non-random way with the actual numbers - from 1 to 9.

make another array with the same number of elements, 9. do another for loop but this time fill each element with an entierely random value - don't even bother restricting or confining it to a particular range, so however the random numbers come - doesn't matter.

then using some sort of sort, like qsort, sort the random array numerically, and in the same way, at the same time in parallel sort the other array. the other array will end up with all your numbers once and only once, in a random order.

Reply With Quote
  #3  
Old June 12th, 2003, 04:28 PM
kavi_s kavi_s is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 24 kavi_s User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You are a genius! Thanks for your wonderful idea! I just tested it, and it's totally cool

Thanks!!

Reply With Quote
  #4  
Old June 12th, 2003, 06:25 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
If you want the range to be from 0..10, then that is 11 unique numbers, not 9.

I think a better way of solving this is like so (if, in fact, you plan on having exactly the same number of elements in the array as the maximum possible unique numbers, which seems to be the case):

Use only one array, of 11 elements. Fill the elements incrementally 0, 1, 2, ... 10 so that the index = the value within the index.

Now, randomly sort the array. How do you do this to make sure every index gets sorted? Swap elements exactly 11 times - the first index is the loop counter (0..10, incrementally), and the second index is random.

Here's the source, in case you didn't follow:

Code:
#include <stdio.h> // printf()
#include <stdlib.h> // rand()
#include <time.h> // time()

int main()
{
	// declaration
	int array[11]; // elements 0..10
	int i;

	// step 1: initialize the array
	for (i=0; i<=10; i++)
		array[i] = i;

	// step 2: sort the array
	srand( (unsigned)time( NULL ) ); // randomize the rand() function
	for (i=0; i<=10; i++)
	{
		int index1 = i;
		int index2 = rand()%11; // x % 11 = 0..10

		// swap elements index1 and index2
		int temp;
		temp = array[index1];
		array[index1] = array[index2];
		array[index2] = temp;
	}

	// show the results.
	for (i=0; i<=10; i++)
		printf("array[%d] = %d\n",i,array[i]);

	return(0);
}

Reply With Quote
  #5  
Old June 12th, 2003, 07:58 PM
kavi_s kavi_s is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 24 kavi_s User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey Jason

That helped a lot too....Thank you very much!! Appreciate it

Kavi

Reply With Quote
  #6  
Old June 13th, 2003, 01:11 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
! i have no idea why i got 9! i was reading about sets and unions etc and wasn't understanding what i was reading just previous to seeing this question, so i blame my confusion on that but then it is actually 10 not 11! (1..10) doesn't matter though.

that is definetely a better way as it doesn't use a 2nd array, so i guess it must be more efficient.

and the array that you fill sequentially then sort randomly - that could contain anything - pointers, characters, other ints, whatever. not necessarily values that reflect the index.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > rand() to generate ARRAY of numbers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT