GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Apr-2005, 17:39
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Generating random numbers


Hi all,

I need to generate and store 2 arrays of 255 unique random integers each. i.e.

CPP / C++ / C Code:
unsigned short int arr1[255];

unsigned short int arr2[255];


Both the arrays should have unique numbers and they should be in the range 1-65535.

Can someone suggest some solution to this problem?

Thanks,
  #2  
Old 25-Apr-2005, 18:23
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
The GenSeq() might be just what you are looking for. It generates a unique array for a deck of cards. You can find it in this thread.

Hope it helps.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 25-Apr-2005, 18:23
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Okey,After googling for sometime, and collecting some bits and pieces of info I wrote this program. It generates 2 arrays of size 255 each with unique integer in the range 1 to 65535.

Anybody want to regress it ?

CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX_NO 65535
#define RANDSIZE 65535

int count=255;

void initrandom(unsigned seed){
 
  static char randstate[RANDSIZE];
  initstate(seed, randstate, RANDSIZE);
}

void compare_arr(unsigned short int arr1[],unsigned short int arr2[])
{
	int i,j;
	int dup=0;
	int dupcount=0;
	for (i=0;i<count;i++)
	  {
		for (j=0;j<i;j++)
		{
		  if (arr1[i] == arr2 [j])
		  {
			dup = 1; 
			dupcount++;
			printf("arr1[%d] = %d\n",i,arr1[i]);
			printf("arr2[%d] = %d\n",j,arr2[j]);
			
		  } else {
			if (!dup) /* set dup to 0 only if its not already set to 1 */
			{
			  dup = 0;
			}
		  }
		}
	  }

  if (dup)
  {
    printf("\n%d Duplicate found\n",dupcount);
  } else {
    printf("\nNo Duplicate found\n");
  }
}

int main(int argc, char *argv[])
{
	unsigned short int arr1[255];
	unsigned short int arr2[255];
	
	int i,j;
	int dup = 0 ;
	int dupcount=0;
	
	printf("%ld\n",RAND_MAX);

	initrandom(time(NULL));
		
	for (j=0;j<count;j++)
	{
		arr1[j]= random();
	}
	
	for (j=0;j<count;j++)
	{
		arr2[j]= random();
	}
	printf("\ns/n\tarr1\tarr2\n\n");
	for (j=0;j<count;j++)
	{
		printf("%d\t%d\t%d\n",j,arr1[j],arr2[j]);
	}
	
	compare_arr(arr1,arr2);
	
	return 0;
}

Thanks,
  #4  
Old 25-Apr-2005, 19:46
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
okey, here is another version. A little customized to my requirement. I have now 3 arrays, each of size 255 and with unique integer. My earlier code in previous post used to generate duplicates with multiple runs. This one takes care of that.

Not very elegent one, but working solution.

CPP / C++ / C Code:
#include <stdio.h>

typedef uint8_t CARD8;
typedef uint16_t CARD16;
typedef uint32_t CARD32;

#define RANDSIZE 65535

typedef enum 
{
	FALSE,
	TRUE
}boolean;

void initrandom(unsigned seed){
 
  static char randstate[RANDSIZE];
  initstate(seed, randstate, RANDSIZE);
}

void rand_gen16(CARD16 arr[],int size)
{
	int j;
	static int first_time = TRUE;

	if (first_time)
	{
		initrandom(time(NULL));
		first_time=FALSE;
	}
	
	for (j=0;j<size;j++)
	{
		arr[j]= random();
	}
}

boolean compare_arr(CARD16 arr1[],CARD16 arr2[],int count)
{
	int i,j;
	int dup=0;
	
	for (i=0;i<count;i++)
	{
		for (j=0;j<i;j++)
		{
		  if (arr1[i] == arr2 [j])
		  {
			dup = 1; 
		  } else {
			if (!dup) /* set dup to 0 only if its not already set to 1 */
			{
			  dup = 0;
			}
		  }
		}
	}

  if (dup)
  {
    return FALSE;
  } else {
    return TRUE;
  }
}

void compare_arr1(CARD16 arr1[],CARD16 arr2[],int count)
{
	int i,j;
	int dup=0;
	int dupcount=0;
	for (i=0;i<count;i++)
	{
		for (j=0;j<i;j++)
		{
		  if (arr1[i] == arr2 [j])
		  {
			dup = 1; 
			dupcount++;
			printf("arr1[%d] = %d\n",i,arr1[i]);
			printf("arr2[%d] = %d\n",j,arr2[j]);
			
		  } else {
			if (!dup) /* set dup to 0 only if its not already set to 1 */
			{
			  dup = 0;
			}
		  }
		}
	}

  if (dup)
  {
    printf("\n%d Duplicate found\n",dupcount);
  } else {
    printf("\nNo Duplicate found\n");
  }
}

void myrandom(CARD16 arr1[],CARD16 arr2[],CARD16 arr3[],int size)
{
	boolean result= FALSE;
	do
	{
		rand_gen16(arr1,size);
		rand_gen16(arr2,size);
		rand_gen16(arr3,size);

		if ((result=compare_arr(arr1,arr1,size)) == FALSE )
		{
			goto END;
		} else if ((result=compare_arr(arr2,arr2,size)) == FALSE)
		{
			goto END;
		} else if ((result=compare_arr(arr3,arr3,size)) == FALSE)
		{
			goto END;
		} else if ((result=compare_arr(arr1,arr2,size)) == FALSE)
		{
			goto END;
		} else if ((result=compare_arr(arr1,arr3,size)) == FALSE)
		{
			goto END;
		} else if ((result=compare_arr(arr2,arr3,size)) == FALSE)
		{
			goto END;
		}
		END:
	} while (result==FALSE);
}

int main(int argc, char *argv[])
{
	unsigned short int arr1[255];
	unsigned short int arr2[255];
	unsigned short int arr3[255];
	int size=255;
	
	myrandom(arr1,arr2,arr3,size);

	printf("Self Compare\n");
	compare_arr1(arr1,arr1,size);
	compare_arr1(arr2,arr2,size);
	compare_arr1(arr3,arr3,size);
	printf("\nCompare with others\n");
	compare_arr1(arr1,arr2,size);
	compare_arr1(arr1,arr3,size);
	compare_arr1(arr2,arr3,size);
	
	return 0;
}

  #5  
Old 26-Apr-2005, 00:25
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
I think this is about the same thing. If I find a "0" I know I have not found a match. I don't really know the proper way to pass the multidim arrays but this seems to work.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXRAND 65535
const int ARRSIZE = 255;
const int ARRNUM  = 3;

void RndInit(){
  static int doorslam = 1;
  static char randstate[MAXRAND];

  if (doorslam){
    initstate(time(NULL), randstate, MAXRAND);
    doorslam = 0;
  }
}

int IsUnique(unsigned int* array[ARRNUM][ARRSIZE], int value){
  int i, j;

  for ( i=0; i<ARRNUM; i++){
    for ( j=0; j<ARRSIZE; j++){
      if (array[i][j] == 0){
        /* We made through all the placed numbers */
        /* without finding a match                */
/*        printf("Unique\n"); */
        return 1;
      }
      else if (array[i][j] == value){
        /* This is a duplicate 'drat!' */
/*        printf("Not Unique\t"); */
        return 0;
      }
    }
  }
  return 0;
}

void GenUnique(unsigned int* array[ARRNUM][ARRSIZE]){
  int searching;
  int newnumber;
  int i, j;

  for ( i=0; i<ARRNUM; i++){
    for ( j=0; j<ARRSIZE; j++){
      /* get a random number and place in array */
      searching = 1;
      do{
        newnumber = (random()%MAXRAND) + 1;
        if (IsUnique(array,newnumber)){
          array[i][j] = newnumber;
          searching = 0;
/*          printf("array[%d][%d] = %d\t", i,j,array[i][j]); */
        }
      }while (searching);
    }
  }
}

void PrintArray(unsigned int* array[ARRNUM][ARRSIZE]){
  int i, j;
  for ( i=0; i<ARRNUM; i++){
    for ( j=0; j<ARRSIZE; j++){
      printf("array[%d][%d] = %d\n", i,j,array[i][j]);
    }
  }
}

int main(){
printf("Random Numbers\n");
  unsigned int array[ARRNUM][ARRSIZE];
  RndInit();
  GenUnique(array);
  PrintArray(array);
  return 0;
}
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #6  
Old 26-Apr-2005, 07:49
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
A few changes to shut up my compiler and some printing additions.

CPP / C++ / C Code:
void GenUnique(unsigned int array[ARRNUM][ARRSIZE]){
  int searching;
  int newnumber;
  int i, j;

  /* Zero the array for multiple runs */
  for (i=0; i<ARRNUM; i++)
    for (j=0; j<ARRSIZE; j++)
      array[i][j] = 0;


void PrintArray(unsigned int array[ARRNUM][ARRSIZE]){
  int i, j;
  for ( i=0; i<ARRNUM; i++){
    for ( j=0; j<ARRSIZE; j++){
      printf("array[%d][%d] = %d\n", i,j,array[i][j]);
    }
  }
}

void PrintArrayNum(unsigned int array[ARRNUM][ARRSIZE],int value){
  int i;
  for ( i=0; i<ARRSIZE; i++){
    printf("array[%d][%d] = %d\n", value,i,array[value][i]);
  }
}


int main(){
//  printf("Random Numbers\n");
  unsigned int array[ARRNUM][ARRSIZE];
  RndInit();
  GenUnique(array);
//  PrintArray(array);
  PrintArrayNum(array,1);
  GenUnique(array);
  PrintArrayNum(array,1);
  return 0;
}
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary number systems: BCD, twos complement, ones complement, etc. machinated Miscellaneous Programming Forum 6 08-Feb-2006 11:51
Linear Search eccoflame C Programming Language 3 19-Apr-2005 09:36
Poker game and random numbers... markersniffer21 C++ Forum 2 06-Nov-2004 09:43
Help with random numbers da_bomb50 MySQL / PHP Forum 3 04-Aug-2004 20:34
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.