7 Replies - 862 Views - Last Post: 06 November 2010 - 10:20 AM Rate Topic: -----

#1 arnell  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 48
  • Joined: 03-November 10

Need a code revised something simple but effective

Posted 03 November 2010 - 01:02 AM

Hey I'm new to programing and I REally need some help with this code fo r c++. its supoosed to be a random number generator that dosent repate numbers. 6 numbers. how do i work the If() repeats than redo and proceed? PLEaSE Help. Thanks and Blessings..
  for (int i = 0; i <1 ; i++)
  {
    srand((unsigned)time(NULL));
    ran(a,6);

 for (int i = 0; i < 6; i++)

  if ( 1!=0; i++)        if (1==0; i--)
  if( 2!=0,1;i++)        if(2==0,1;i--)
  if( 3!=0,1,2,i++)       if( 3==0,1,2,i--)
  if(  4!=0,1,2,3;i++)    if(  4==0,1,2,3;i--)
  if( 5!=0,1,2,3,4;i++)    if( 5==0,1,2,3,4;i--)
  if( 6!=0,1,2,3,4,5;i++)  if( 6==0,1,2,3,4,5;i--)

  srand((unsigned) time(NULL));


   {       cout << a[i] << " ";
         cout << endl;
         }}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 November 2010 - 03:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need a code revised something simple but effective

#2 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Need a code revised something simple but effective

Posted 03 November 2010 - 01:12 AM

Your if clauses look very odd.Try to use something simpler:

bool occupied[6];
for(int i=0;i<6;i++)
  occupied[i] = false;
int a[6];
srand(time(NULL));
for(int i=0;i<6;i++)
{
  int r = rand() % 6;
  while(occupied[r])
    r = rand() % 6;
  a[i] = r;
  occupied[r] = true;
}



The idea is to have an extra array called "occupied". As soon as you get a new random number you mark that number as occupied.

This post has been edited by JackOfAllTrades: 03 November 2010 - 03:36 AM
Reason for edit:: Removed unnecessary quote. There's really no need to quote the post immediately prior to yours.

Was This Post Helpful? 0
  • +
  • -

#3 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Need a code revised something simple but effective

Posted 03 November 2010 - 01:18 AM

Did you read the big pink box that appears when you type a post. It asks you to post your code in code tags.
Please edit your post and put your code in code tags, like this: Attached Image

It will improve the readability and make other's life easier. And will help you in getting a quick, expert response.

And did you even compile this code once and tried to debug it on your own. Or you have directly come here asking for help thinking that someone will fix it for you

This post has been edited by newclearner: 03 November 2010 - 01:41 AM

Was This Post Helpful? 1
  • +
  • -

#4 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Need a code revised something simple but effective

Posted 03 November 2010 - 01:36 AM

View Postarnell, on 03 November 2010 - 12:02 AM, said:

  if ( 1!=0; i++)        if (1==0; i--)
  if( 2!=0,1;i++)        if(2==0,1;i--)
  if( 3!=0,1,2,i++)       if( 3==0,1,2,i--)
  if(  4!=0,1,2,3;i++)    if(  4==0,1,2,3;i--)
  if( 5!=0,1,2,3,4;i++)    if( 5==0,1,2,3,4;i--)
  if( 6!=0,1,2,3,4,5;i++)  if( 6==0,1,2,3,4,5;i--)



You can't put multiple expressions into an if-clause.
Either if(1!=0) or if(i++) or if(1!=0 && i++) would be legal.
What do you expect from if(1!=0)? What do you expect from if(6!=0,1,2,3,4,5)?
1 is always unequal to zero. So you don't need to check the equality with if.
and (0,1,2,3,4,5) is evaluated to the binary maximum of those numbers (in this case 5).

Just try those examples:

int a = -10;
int b = 7;
int c= 9;
cout << (a,b,c) << endl;



if(1!=0) cout << "Surprise: 1 is unequal to 0" << endl;
if(1!=0,1) cout << "Surprise: 1 is unequal to (0,1)" << endl;
if(1!=-1,0,1) cout << "Surprise: 1 is unequal to (-1,0,1)" << endl;


Was This Post Helpful? 0
  • +
  • -

#5 arnell  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 48
  • Joined: 03-November 10

Re: Need a code revised something simple but effective

Posted 04 November 2010 - 09:08 AM

Thank alot! I appreciate the help. I do however still have a question. How does it check to make sure the current number has not been produced before. I understand this code to occupy the number with a random number the same as an array intiallized and set and then given the objective such as for array srand(seeded). I can then print using a printf("%d\n",rand()50); (50 since Im setting that as max;0-50) but what I need is a way to get into the program and have the program physicly check the numbers like a guy buying differnt colored shirts for Monday though Saturday. Really, thanks again. now im going back to work.
R.H
Was This Post Helpful? 0
  • +
  • -

#6 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Need a code revised something simple but effective

Posted 04 November 2010 - 11:51 AM

  while(occupied[r])
    r = rand() % 6;



As long as r is already occupied, create a new r.
To have this random number creator encapsulated in a function, you need to remember the occupied numbers from previous function calls. I suggest to use a class for this not-so-easy task.

class RandomCreator
{
public:
  RandomCreator(int _n) 
    : n(_n), occupied(n,false) // initialize our occupied-array
  {
    srand(time(NULL));
  }
  // create an unique random number 
  int unique_rand()
  {
    int r = rand() % n; // create random number in [0,n[
    while(occupied[r])
      r = rand() % n; // create new random number in [0,n[
    occupied[r] = true;
    return r;  
  }
private:
  vector<bool> occupied;
  int n; // the range
};



The usage is quite simple:

int main()
{
  RandomCreator random(50);
  cout << random.unique_rand() << endl;
  cout << random.unique_rand() << endl;
  cout << random.unique_rand() << endl;
  return 0;
}



Be sure not to call unique_rand more than 50 times, otherwise you will have an infinite loop. If you are looking for a more C-stylish solution, do it yourself, I have no simple idea :)

This post has been edited by Djabby: 04 November 2010 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Need a code revised something simple but effective

Posted 05 November 2010 - 03:35 AM

The simple way to do the original question.
(Just because I think the actual challenge got lost in the stream of the thread and creation of far too complex answers.)

Create a storage array with 6 elements.
Create random numbers in an infinite loop.
As you create each random number check if it already in the array, if it isn't add it to the array and increment your counter.
If it is already in the storage array do nothing and get another random number.
When you have 6 numbers in the array exit the infinite loop.

I am sure you can work out how to check if a number is already in the array if you think about it for 10 minutes.
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Need a code revised something simple but effective

Posted 06 November 2010 - 10:20 AM

You are calling srand far too often. srand should be called exactly ONCE for the lifetime of your program, meaning that the best place to put it is near the top of your main() function

int main()
{
     srand(time(NULL));

    /*
     *    etc.
     */

} 


Every time you call srand, you reset the seed which rand() uses, and you will find that you get an awful lot of repetition.

If you would like to read up on rand(), have a look here: http://www.eternally...w_art_rand.aspx
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1