4 Replies - 880 Views - Last Post: 30 May 2009 - 02:51 PM Rate Topic: -----

#1 shoot to thrill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-May 09

random array generation (w/ specific parameters)

Posted 30 May 2009 - 10:10 AM

Greetings all!

I know this is the first time I've posted here, however any help on the issue I'm having would be massively appreciated, I've been trying for a while to crack this now but with limited success!!

I am essentially trying to implement code that will generate an array of random integer values (S[ind]) for 'co-use' with the values of an existing array A[a] so that these random values can be used as references in forming array B[a] (I shall try to explain this better below if it seems a bit confusing). In this case, 'ind' and 'a' are parameter inputs selected by the user (ind<a as a rule).

It should check the randomly produced values so that [this is the part Im having problems with I believe];
- the same random value doesn't occur twice within S[ind] for the individual array element in A[a] that 'S' is being generated for i.e. S[0] != S[1, 2...(ind-1)] and S[1] != S[0, 2...(ind-1)] and so on.. [Condition 1, see code below]
- the random array 'S', that is generated for each value of B[a] doesnt contain a value that is the same as the reference of the 'B' element that we are generating for. i.e. for B[1], 1 doesnt come out in S[ind] [Condition 2]

The idea is that the randomly generated values are then used as array references for A[a] in generating B[a]. The latter is done by adding up all the numerical values of A[a] that are referenced by our random values then checking against certain conditions (not necessary to mention here), and then at the end of the program B[a] = A[a] so that the cycle continues etc

Here's what I've done upto now, I've been writing this in a seperate source file from the rest of my code to make sure it's right before cutting/pasting etc.


#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime> 
#include <windows.h> 


using namespace std;

int main()
{
	
int a = 7;
int ind = 3;
int S[ind], B[a]; 
int A[7] = {1, 1, 1, -1, -1, 1, -1};   

srand(time(NULL));
	
for (int g=0;g<ind;g++)
	{
	S[g] = rand()%(a-1);
	
	do {
	S[g] = rand()%(a-1);
	cout<<"2nd\n";
	   } while ((A[(S[g])]) == A[g]);	   //Condition 2(?)
	for (int j=0;j<ind;j++)			   
		{	 
		 if (g != j)		  
			{   
				do {
				S[g] = rand()%(a-1);
				cout<<"1st\n";
				   } while (S[g] = S[j]);   //Condition 1 - checks all values 
			}								//against one-another(?)
		}
	}
		
for (int r=0;r<ind;r++)	 //Prints formed array of random values 
	{						  //(it doesnt get to this stage)
	printf("\n", S[r]);	
	}
 
cin.get();
}



In this case, the cout's are used as indicators when I run the program of what recurrence is happening. Is the way Im testing the generated values correct? The program compiles fine, however I get an infinite loop of cout's when this is not expected, possibly because one or both of the conditional statements are met all the time? I think this is just down to limitations of my understanding of C++ as it stands (hopefully to be changed with enough time and patience haha)

Just to clarify, acceptable random array's may be;
(for B[0]) S[] = {1, 2, 4, 7} (ind = 4, a>7)
(for B[1]) S[] = {0, 3, 5, 6}

Not acceptable;
(for B[2]) S[] = {0, 2, 4, 5} or S[] = {1, 1, 3, 5}

These references are then used in this manner (syntax may be incorrect);
B[0] = A[S[0]] + A[S[1]] ...A[S[ind-1]]...(is this a good way of adding up the values, by using a for loop?)

Many thanks in advance for any help on this, it's been a bit of a nightmare (Im sure gettin to grips with new coding techniques always is haha)
Regards, Ryan

Compiler: Dev-C++
OS: XP Pro

Is This A Good Question/Topic? 0
  • +

Replies To: random array generation (w/ specific parameters)

#2 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: random array generation (w/ specific parameters)

Posted 30 May 2009 - 10:45 AM

This line will give you an infinite loop:

while (S[g] = S[j]); 



Instead of the assignment operator, I think you want the comparison operator i.e.:

while (S[g] == S[j]); 


Was This Post Helpful? 1
  • +
  • -

#3 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: random array generation (w/ specific parameters)

Posted 30 May 2009 - 01:11 PM

think before coding,

thus an algorithm is been discovered !

for any array of any size we can randomize its elements to a limit !

void RandomizeArray(int* &arr, int limit, bool zero)
{
	int size = sizeof(arr);
	int z = (int)(!zero);
	srand(time(NULL));
	
	for(int i=0;i<size;i++)
		arr[i] = z+rand()%limit;
}



simply as that ...
Was This Post Helpful? 0
  • +
  • -

#4 shoot to thrill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-May 09

Re: random array generation (w/ specific parameters)

Posted 30 May 2009 - 02:03 PM

View Postapw5020, on 30 May, 2009 - 09:45 AM, said:

This line will give you an infinite loop:

while (S[g] = S[j]); 



Instead of the assignment operator, I think you want the comparison operator i.e.:

while (S[g] == S[j]); 



Ah, thanks for that. yea that seems to have gotten rid of the infinite loop issue, tho the ouptput Im getting still doesnt look right. It's typically something like this (varies from run to run, which ofc is a good thing haha);

1st
2nd
2nd
2nd
1st
1st
2nd
2nd
...(indefinitely)
[and then the series of random values to be used as array references]
1 (S[0])
1 (S[1])
4 (S[2]) ...

the problem is recurring values are coming out. any ideas why and how I could fix this? could I possibly place the conditionals into the same do-while? Thanks for the help, Regards - Ryan
Was This Post Helpful? 0
  • +
  • -

#5 shoot to thrill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-May 09

Re: random array generation (w/ specific parameters)

Posted 30 May 2009 - 02:51 PM

View PostPlus, on 30 May, 2009 - 12:11 PM, said:

think before coding,

thus an algorithm is been discovered !

for any array of any size we can randomize its elements to a limit !

void RandomizeArray(int* &arr, int limit, bool zero)
{
	int size = sizeof(arr);
	int z = (int)(!zero);
	srand(time(NULL));
	
	for(int i=0;i<size;i++)
		arr[i] = z+rand()%limit;
}



simply as that ...


ah ok thanks plus. Im still not sure exactly how I can implement this into my code though, could you possibly help me there using the parameters I have above? I dont think Im at the level to understand exactly what's going on here tbf, have had a look through this book I have infront of me and there isnt anything about this function/algorithm. I have to make sure that the random arrays being generated here for individual use in the latter calculation of B[a ] dont contain value 'a' if you know what I mean? this is what Im worried about here. thanks anyway :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1