//Result
X[0]= 0
X[1]= 3
X[2]= 4208527
X[3]= 4
X[4]= 4208527
Press any key to continue . . .
//Program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{ int x=0;
int X[5];
srand (time(NULL));
X[0] =rand()%5;
x=rand()%5;
if (x!=X[0])
X[1]=x;
x=rand()%5;
if ((x!= X[0])&& ( x!=X[1]))
X[2]=x;
x=rand()%5;
if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
X[3]=x;
x=rand()%5;
if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
X[4]=x;
for (int i=0; i<5 ; i++)
printf ("X[%d]= %d \n",i, X[i]);
printf ("\n");
return 0;
}
One point : You are not storing random numbers if the rand%POS returns a previously
selected stored number. You need to put a while loop around the 2nd-5th number
generations.
Thats part of it.
The rest will be laboriously picked on no doubt.
--
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c
You're program is needlessly complex because you've avoided loops for
some reason. The strange results you've observed are because you
don't store any number when the return value of rand() is already
equal to any previous element of X. Thus the printf loop prints
whatever "random" value that happens to be in those cells. Here's
another version. Notice the cast of the return value of time().
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ARR_SIZE 5
int occurs(int *arr, size_t n, int val) {
size_t i;
for (i = 0; i < n; i++)
if (arr[i] == val) return 1;
return 0;
}
int main(void) {
int arr[ARR_SIZE], num;
size_t i;
memset(arr, 0, sizeof arr);
srand((unsigned)time(NULL));
for (i = 0; i < ARR_SIZE; i++) {
do {
num = rand() % 5;
} while (occurs(arr, i, num));
arr[i] = num;
}
for (i = 0; i < ARR_SIZE; i++)
printf("arr[%zu] == %d\n", i, arr[i]);
return 0;
}
> Hi,
> I am trying to print 5 unique random numbers 0 to 4. [...]
Here's also a shorter program that meets your spec;-)
#include <stdio.h>
int main(void) {
printf("0, 1, 2, 3, 4\n");
return 0;
}
You really have been doing your best to enter the c.l.c arsehole clique
Santosh. Well done.
You probably missed:
- the smiley in Santosh' post
- the fact that Santosh posted a helpful answer in a parallel subthread
"santosh" <santo...@gmail.com> wrote in message
news:hmaovr$iuj$2...@news.eternal-september.org...
Yes, see my other post in the thread. That does provide a solution to
your requirements I believe.
// 2nd run
arr[zu] == 0
arr[zu] == 1
arr[zu] == 2
arr[zu] == 3
arr[zu] == 4
Press any key to continue . . .
Hi Santosh,
Why zu? Why not array numbers 1,2,3 etc.
Why the 1st run and 2 run gives identical results? I dont see the random
sequence of the numbers.
I was expecting
arr[0] == 4
arr[1] == 2
arr[2] == 0
etc
etc
Rgds,
Khoon.
This cannot be the output from the source I posted. It shouldn't
print "zu" but the array element number. Perhaps your compiler
doesn't recognise %zu. I believe that might be the problem here.
The zu conversion specifier for printf is to print values of type
size_t. It was added with the C99 standard. If your compiler doesn't
support them, as seems to be the case here, just change the %zu to
%lu in printf and cast the corresponding argument to unsigned long.
Here is a modified version. Try this and see if you still get these
strange output.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ARR_SIZE 5
int occurs(int *arr, size_t n, int val) {
size_t i;
for (i = 0; i < n; i++)
if (arr[i] == val) return 1;
return 0;
}
int main(void) {
int arr[ARR_SIZE], num;
size_t i;
memset(arr, 0, sizeof arr);
srand((unsigned)time(NULL));
for (i = 0; i < ARR_SIZE; i++) {
do {
num = rand() % 5;
} while (occurs(arr, i, num));
arr[i] = num;
}
for (i = 0; i < ARR_SIZE; i++)
printf("arr[%lu] == %d\n", (unsigned long)i, arr[i]);
return 0;
}
> Hi Santosh,
arr[0] == 3
arr[1] == 1
arr[2] == 2
arr[3] == 0
arr[4] == 4
Press any key to continue . . .
Yes. Now it is working perfectly. Very very well.
I am using Microsoft Visual Studio using the C++ platform to do my C.
I am a beginner student on C in College.
I am awed and amazed by the depth and breadth of your enormous knowledge
in C programming.
I will need sometime to spend digesting and learn from your program.
100 more complex than my ealier program. Ha..ha..
Thank you very very much for your great help.
Rgds,
khoon
Here's one along similar lines, not shorter but almost as simple:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int numbers[][5]={
{0,1,2,3,4},
{0,1,2,4,3},
{0,1,3,2,4},
{0,1,3,4,2},
{0,1,4,2,3},
{0,1,4,3,2},
{0,2,1,3,4},
{0,2,1,4,3},
{0,2,3,1,4},
{0,2,3,4,1},
{0,2,4,1,3},
{0,2,4,3,1},
{0,3,1,2,4},
{0,3,1,4,2},
{0,3,2,1,4},
{0,3,2,4,1},
{0,3,4,1,2},
{0,3,4,2,1},
{0,4,1,2,3},
{0,4,1,3,2},
{0,4,2,1,3},
{0,4,2,3,1},
{0,4,3,1,2},
{0,4,3,2,1},
{1,0,2,3,4},
{1,0,2,4,3},
{1,0,3,2,4},
{1,0,3,4,2},
{1,0,4,2,3},
{1,0,4,3,2},
{1,2,0,3,4},
{1,2,0,4,3},
{1,2,3,0,4},
{1,2,3,4,0},
{1,2,4,0,3},
{1,2,4,3,0},
{1,3,0,2,4},
{1,3,0,4,2},
{1,3,2,0,4},
{1,3,2,4,0},
{1,3,4,0,2},
{1,3,4,2,0},
{1,4,0,2,3},
{1,4,0,3,2},
{1,4,2,0,3},
{1,4,2,3,0},
{1,4,3,0,2},
{1,4,3,2,0},
{2,0,1,3,4},
{2,0,1,4,3},
{2,0,3,1,4},
{2,0,3,4,1},
{2,0,4,1,3},
{2,0,4,3,1},
{2,1,0,3,4},
{2,1,0,4,3},
{2,1,3,0,4},
{2,1,3,4,0},
{2,1,4,0,3},
{2,1,4,3,0},
{2,3,0,1,4},
{2,3,0,4,1},
{2,3,1,0,4},
{2,3,1,4,0},
{2,3,4,0,1},
{2,3,4,1,0},
{2,4,0,1,3},
{2,4,0,3,1},
{2,4,1,0,3},
{2,4,1,3,0},
{2,4,3,0,1},
{2,4,3,1,0},
{3,0,1,2,4},
{3,0,1,4,2},
{3,0,2,1,4},
{3,0,2,4,1},
{3,0,4,1,2},
{3,0,4,2,1},
{3,1,0,2,4},
{3,1,0,4,2},
{3,1,2,0,4},
{3,1,2,4,0},
{3,1,4,0,2},
{3,1,4,2,0},
{3,2,0,1,4},
{3,2,0,4,1},
{3,2,1,0,4},
{3,2,1,4,0},
{3,2,4,0,1},
{3,2,4,1,0},
{3,4,0,1,2},
{3,4,0,2,1},
{3,4,1,0,2},
{3,4,1,2,0},
{3,4,2,0,1},
{3,4,2,1,0},
{4,0,1,2,3},
{4,0,1,3,2},
{4,0,2,1,3},
{4,0,2,3,1},
{4,0,3,1,2},
{4,0,3,2,1},
{4,1,0,2,3},
{4,1,0,3,2},
{4,1,2,0,3},
{4,1,2,3,0},
{4,1,3,0,2},
{4,1,3,2,0},
{4,2,0,1,3},
{4,2,0,3,1},
{4,2,1,0,3},
{4,2,1,3,0},
{4,2,3,0,1},
{4,2,3,1,0},
{4,3,0,1,2},
{4,3,0,2,1},
{4,3,1,0,2},
{4,3,1,2,0},
{4,3,2,0,1},
{4,3,2,1,0}};
int main(void)
{
int *X;
srand (time(NULL));
X=numbers[rand()%120];
printf ("X = (%d,%d,%d,%d,%d)\n",X[0],X[1],X[2],X[3],X[4]);
return 0;
}
--
Bartc
> I am trying to print 5 unique random numbers 0 to 4.
The best way to phrase this is that you want to generate a random
permutation of the numbers 0 to 4.
<snip code>
You've had your problem explained and a solution proposed, but there
is a simpler solution which is to put 0 to 4 into your array and use
random numbers to shuffle the elements:
http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
--
Ben.
> Yes. Now it is working perfectly. Very very well.
> I am using Microsoft Visual Studio using the C++ platform to do my
> C.
And MS has very poor support for C99 it seems. If you have time, you
might also investigate alternative compilers for Windows like
Cygwin/MinGW, PellesC, lcc-win32 and so on. Most of these support
most of C99, so are better than Visual C in this aspect.
> I am a beginner student on C in College.
> I am awed and amazed by the depth and breadth of your enormous
> knowledge in C programming.
> I will need sometime to spend digesting and learn from your
> program.
> 100 more complex than my ealier program. Ha..ha..
Thanks for your praises, but you've massively overestimated my skill
in C. Compared to many of the participants in this group, I'm sure we
both would seem like beginners.
So this is a very good place to learn the subtler aspects of C, and
programming in general. You'll get insights here that you're unlikely
to see in most C books.
> Thank you very very much for your great help.
> Rgds,
> khoon
You're welcome.
> santosh <santo...@gmail.com> writes:
>
>> Tadpole <tsk...@streamyx.com> writes:
>>
>>> Hi,
>>> I am trying to print 5 unique random numbers 0 to 4. [...]
>>
>> Here's also a shorter program that meets your spec;-)
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> printf("0, 1, 2, 3, 4\n");
>> return 0;
>> }
>>
>
> You really have been doing your best to enter the c.l.c arsehole
> clique Santosh. Well done.
Thank you master. I seek only to serve you;-)
/* BEGIN new.c output */
arr[0] is 4
arr[1] is 1
arr[2] is 3
arr[3] is 2
arr[4] is 0
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define N 5
void
shuffle(int *array, int n);
int
main(void)
{
int arr[N];
int x;
puts("/* BEGIN new.c output */\n");
for (x = 0; N > x; ++x) {
arr[x] = x;
}
shuffle(arr, x);
for (x = 0; N > x; ++x) {
printf("arr[%d] is %d\n", x, arr[x]);
}
puts("\n/* END new.c output */");
return 0;
}
void
shuffle(int *array, int n)
{
int i, r;
array[0] = 0;
for (i = 1; n > i; ++i) {
r = rand() % (i + 1);
array[i] = 0;
array[i] = array[r];
array[r] = i;
}
}
/* END new.c */
--
pete
Why not stored?
In my program, everytime when x is generated a random number that
qualifies the if statement, it is stored as X[i] = x:;
In the first instance it is stored in X[0] then the next instance X[1] =
x;
Why is the x value generated not stored? How to make the program that
it stores permanently and not disappears whcn it comes to the printf
statement? Causing the printf statement to print something else?
Rgds,
Khoon.
What happens when
when x is generated a random number that
*doesn't* qualifies the if statement?
> In the first instance it is stored in X[0] then the next instance X[1] =
> x;
> Why is the x value generated not stored? How to make the program that
> it stores permanently and not disappears whcn it comes to the printf
> statement? Causing the printf statement to print something else?
> Rgds,
> Khoon.
>
>
>
>
>
>
>
--
pete
Hi Bartc,
Your method is good and workable. But not practical.
It is OK for 5 numbers. But what if it is 25 numbers?
My college assignment is to solve the Travelling Saleman Problem using the
Hill Climbing algorithm.
There are 25 cities. So eventually, I have to deal with 25 numbers.
The salesman will need to travel at random through 25 cities and back.
I am developing the algorithm working from first principle starting from
the basic.
Thanks for your suggestion.
Most appreciate.
Rgds
Khoon.
/*
** I've made the smallest amount of changes to your program
** to implement the algorithm that I think you want.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{ int x=0;
int i;
int X[5];
srand (time(NULL));
X[0] =rand()%5;
do {
x = rand() % 5;
X[1] = x;
} while (x == X[0]);
do {
x = rand() % 5;
X[2] = x;
} while (x == X[0] || x == X[1]);
do {
x = rand() % 5;
X[3] = x;
} while (x == X[0] || x == X[1] || x == X[2]);
do {
x = rand() % 5;
X[4] = x;
} while (x == X[0] || x == X[1] || x == X[2] || x == X[3]);
for (i=0; i<5 ; i++)
printf ("X[%d]= %d \n",i, X[i]);
printf ("\n");
return 0;
}
--
pete
OH YES !! Now I get it.
The memory cell is empty because nothing is stored there because the if
statement is not fulfilled.
Thanks a zillion.
This alogrithm must be in a continuous while loop so that the if
statement has a chance to be fulfilled.
Rgds,
khoon.
> Your method is good and workable. But not practical.
> It is OK for 5 numbers. But what if it is 25 numbers?
No problem, just use:
int numbers[][25]={
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,23},
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,22,24},
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,22},
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,22,23},
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,23,22},
<snip 15,511,210,043,330,985,983,999,994 further lines>
No, you should use some random shuffle method in this case (already
mentioned in the thread). Although I don't know how that would apply to your
travelling salesman problem.
--
bartc
Thanks Pete,
However you forgot to put in srand..
I have added it and now the program runs perfectily.
I will need some time to run through the algorithm.
There are a few commands new to me.
Rgds
Khoon.
You're welcome.
Please feel free to ask questions on any and all posted code.
--
pete
I am thinking for the annealing part I will begin the analysis by shuffling
all the cities. So the salesman keeps travelling randomly through all the
cities and work out the loop having the shortest distance he can discover.
This is important because the distance from city 0 to city 1 is different
from city 1 to city 0. Then gradually he refines his loop by exchanging
cities to work out the Local Maxima or Local Peak in his Hill Climbing.
As this is not using "brute force method" we may not get the Global Maxima.
This is OK because the assignment ask for 1000 loops by the Saleman. The
assignment is also is a test on the programming skill other than solving the
Saleman problem.
Rgds,
Khoon
You might also have a look at clc faq question 13.19:
Your suggestion is absolutely brilliant.
It exactly solves my problem with only just a few programming lines. As
shown below: I shuffle 20 times.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{ int A[]= {0,1,2,3,4};
srand (time(NULL));
int x,y,z;
for (int i=0; i<20 ;i++)
{ x =rand()%5;
y =rand ()%5;
printf ("%d %d \n",x,y);
z= A[x];
A[x]=A[y];
A[y]=z;
}
printf ("\n");
for (int i=0; i<5; i++)
printf ("A[%d]= %d\n",i,A[i]);
return 0;
}
Is there one "include" statement that I can write to substitute the 3 "
#include " statement in the header?
Meaning #include < XXX.h > where XXX represent the statement I am
searching for?
Rgds,
Khoon
> [excerpting code...]
> for (int i=0; i<20 ;i++)
> { x =rand()%5;
> y =rand ()%5;
> printf ("%d %d \n",x,y);
> z= A[x];
> A[x]=A[y];
> A[y]=z;
> }
Except that the code you wrote is NOT the suggested code
(though it shares the general form "for ... rand ... swap"
and thus has a superficial similarity). That your version
approaches randomness (as number of "shuffles" increases)
while the Knuth/Fisher/Yates method is "perfect" with just
N-1 swaps is discussed to death twice a year or so in the
comp.programming ng.
The interesting question here, I think, is how you
followed the "absolutely brilliant" suggestion (which does
point to the ordinary correct code), and ended up with your
version?
BTW, your completed TSP program will need TWO shufflings:
the initialization, and the single-swaps for each step
in your hill-climbing. It appears you might be trying
to reuse the code for the latter to do the former.
Since the key Knuth/Fisher/Yates loop is just 4 lines
of code (see below), one wonders what's the point of
such a reuse.
> for(i = 0; i < nvalues-1; i++) {
> int c = randrange(nvalues-i);
> int t = a[i]; a[i] = a[i+c]; a[i+c] = t; /* swap */
> }
James Dow Allen
Since you have #included <stdlib.h> and <time.h>, which provide proper
declarations for time(), srand(), and time_t, what's the point of adding
an extra (ugly, IMO) explicit cast for the argument to srand()?
srand() is declared with a prototype which says that its argument is an
(unsigned int), and time() is declared to return time_t, which is declared
in <time.h> as an arithmetic type, so the conversionm if necessary, to
(unsigned) will take place automatically. Why muddy up the code by doing
it manually?
--
Morris Keesan -- mke...@post.harvard.edu
(I hope the OP is still around and reading .... )
In my thinking the notion of "empty memory cell" is -- misleading,
though perhaps there are circumstances in which it's a meaningful
description.
My thinking is that all memory cells contain *something*, some
pattern of ones and zeros, but what those ones and zeros mean,
if indeed they mean anything, depends on the program that's
accessing them. Better to consider that a memory cell that hasn't
been explicitly set to some known value contains random junk.
(I await correction by the experts. :-)? )
> Thanks a zillion.
> This alogrithm must be in a continuous while loop so that the if
> statement has a chance to be fulfilled.
[ snip ]
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.