• C

# Assigning a new number if it has already been used.

Ok well my code is nearly there. I have done this a number of different ways but I really want figure out how to accomplish it this way (a sort of personal goal thing). So what I want to do is assign a new number to the current point in the 4 element integer array if the number has already been used - see the code below. It can tell if the number has already been used because it has been "flagged" either 0 or 1 accordingly. I am sure you experts out there can give us a hint. I am sure I wanna put something in the else statement that I have left blank but I have tried a few things that didn't work and can't think of any other way of doing it!

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SUCCESS 0
#define ERROR 1

int GenerateRandomNumber (void)
{
int i, j, s;
int t, r[4];
int c[10] = {0, 0, 0, 0, 0,
0, 0, 0, 0, 0};

srand ((unsigned) time(0));
for (i = 0 ; i < 4 ; i++)
{
t = rand() %9;
c[t] = 1;
s = 0;
for (j = 0 ; j < 10 ; j ++)
{
if (c[j] == 1)
{
r[i] = t;
s++;
if (s == 4)
{
break;
}
else
{
}
}
}
printf ("%d", r[i]);
}
printf ("\n%d", s);

return SUCCESS;
}

int main (void)
{

if (GenerateRandomNumber())
{
fprintf (stderr, "Error calling function RandomNumer\n");

return ERROR;
}
printf ("\n");

return SUCCESS;
}

Regards

Anewbis_
###### Who is Participating?

Commented:
Lemme guess: You're trying to create four different numbers?

If you want to keep on creating a new number until you have four different ones, you really want to use a while {..} loop

e.g.
for (j=1 to 4)
{
flag=0;
while (!flag)
{
t=random number;
if (!c[t])
{
c[t]=1;
flag=1;
}
}
}

Is that the sort of thing you're looking for?
0

Commented:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SUCCESS 0
#define ERROR 1

int GenerateRandomNumber (void)
{
int i, j, s;
int t, r[4];
int c[10] = {0, 0, 0, 0, 0,
0, 0, 0, 0, 0};

srand ((unsigned) time(0));
for (i = 0 ; i < 4 ; i++)
{
t = rand() %9;
c[t] = 1;
s = 0;
for (j = 0 ; j < 10 ; j ++)
{
if (c[j] == 1)
{
//r[i] = t;
r[s] = t; // because i will always be the same in this loop
s++;
if (s == 3) // not 4, 0 - 3
{
break;
}
else // don't need else
{
}
}
}
printf ("%d", r[i]);
}
printf ("\n%d", s);

return SUCCESS;
}

int main (void)
{

if (GenerateRandomNumber())
{
fprintf (stderr, "Error calling function RandomNumer\n");

return ERROR;
}
printf ("\n");

return SUCCESS;
}
0

Author Commented:
Er!! That doesn't work! Do you understand what I am trying to do?
0

Author Commented:
Yeah I'd kinda done it that way before, but the flagging thing worked well I just wanted to do it by flagging the 0's to 1.

Thanx
0

Commented:
Can I also take the liberty of pointing out that rand%9 will give you a value from 0 to 8, so c[9] is not going to be referenced?

Incidentally

do
{
t=random number%9; /* 10? */
}
while (c[t]!=0);
c[t]=1;

will also do the trick.

Thanks anyway.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.