# Thread: How to generate random number without repetition?

1. Originally Posted by pheininger
Old:
Code:
`   double arr[30][30];`
New:
Code:
`   double arr[30][30]={0.0};`

Old:
Code:
```          i= (int) rand()0;
arr[i][j]= (double) rand()/RAND_MAX;
if (arr[i][j] > 0){
m[j]++;
}```
New:
Code:
```          i= (int) rand()0;
if (arr[i][j] == 0.0) {
arr[i][j]= (double) rand()/RAND_MAX;
if (arr[i][j] > 0.0) {
m[j]++;```
Thank you so much sir!!!! It's work

2. I don't see how that answers your question about generating random numbers without repetition. But if you're happy, I'm happy.

3. Originally Posted by oogabooga
It does if the OP is just trying to avoid writing to the same index randomly more than once, but does not care if the value assigned is a duplicate of one at some other index (which is pretty unlikely anyway considering the range is a double > 0.0 and <= 1.0). IMO opinion it would be better to create an array of possible indexes and shuffle that, then pop them off one at a time tho.

Hopefully Suntang used %30 for the index and not the code posted .

4. My understanding was that the OP wanted 4 random "reviewers" (numbers from 0 to 29, without repetition for any particular "object") to review each object, and that a "review" was a random number from 0.0 to 1.0. But I could have been reading into the situation.

5. Originally Posted by oogabooga
My understanding was that the OP wanted 4 random "reviewers" (numbers from 0 to 29, without repetition for any particular "object") to review each object, and that a "review" was a random number from 0.0 to 1.0. But I could have been reading into the situation.
That's what the new code will do, look:

Code:
```i= (int) rand()0; // [sic]
if (arr[i][j] == 0.0) {
arr[i][j]= (double) rand()/RAND_MAX;
if (arr[i][j] > 0.0) {
m[j]++;```
presuming m[j] is the count for that column, it only gets incremented if arr[i][j] was 0.0 to start with and then set to >0.0. The problem before was that repeated values for i meant the same element might get set twice, and count each time. Now, there is a check to see if the element has already been set, and if so, there is no second set and no increment of the count.

6. OK. I see it now.

7. Hello, all sir!! I still have one more question to ask, when i increase the number of i and j to (1500) each. i got the error call " Class StackOverflow", then i decided to use calloc() function like below:
Code:
```double** arr;
int rows = 1500;
int columns = 1500;
int m[1500];
int i,j,r;
i=0;
arr= (double **) malloc(rows*sizeof(double*));
for(r=0; columns; r++)
arr[r]=(double*)malloc(columns*sizeof(double));
srand((unsigned)time(NULL));
for(j=0; j<columns; j++){
m[j]= 0;
.......
.......
}
free(arr);
getch();
}```
But at the result i got only some data not all. What's wrong in my code sir? Any idea?

8. Code:
`for(r=0; columns; r++)`
Is obviously an infinite loop, since if columns is 1500, the middle condition will always be true. So this is not your actual code, this is some version of it. Making it impossible to say "what's wrong", because you did not post the real code. Please do not do that in the future, it is a waste of time for everybody. If you can't post the actual code, write a compilable program that re-creates the problem, ie, one the can actually be compiled and run to demonstrate the error or problem.

Code:
`free(arr);`
That's a memory leak. You have to free each row in arr first, just as you allocated each row.

9. Originally Posted by MK27
Code:
`for(r=0; columns; r++)`
Is obviously an infinite loop, since if columns is 1500, the middle condition will always be true. So this is not your actual code, this is some version of it. Making it impossible to say "what's wrong", because you did not post the real code. Please do not do that in the future, it is a waste of time for everybody. If you can't post the actual code, write a compilable program that re-creates the problem, ie, one the can actually be compiled and run to demonstrate the error or problem.

Code:
`free(arr);`

That's a memory leak. You have to free each row in arr first, just as you allocated each row.

I'm so sorry sir, this is the hold code:

Code:
```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
double** arr;
int rows = 1500;
int columns = 1500;
int m[1500];
int i,j,r;
i=0;
arr= (double **) malloc(rows*sizeof(double*));
for(r=0; columns; r++)
arr[r]=(double*)malloc(columns*sizeof(double));
srand((unsigned)time(NULL));
for(j=0; j<columns; j++){
m[j]= 0;
while(m[j] < 15){
i= (int) rand()%1500;
if (arr[i][j] == 0.0) {
arr[i][j]= (double) rand()/RAND_MAX;
if(arr[i][j] > 0){
m[j]++;
printf("arr[%d][%d] = %lf\t", i, j, arr[i][j]);
printf("\n");
}
}
}
}
free(arr);
getch();
}```

10. Read this > FAQ > Casting malloc - Cprogramming.com

Take MK's advice about free()ing your array. When you allocate, "arr" is an array of pointers. Assuming you allocate a "3x3" array of 8-byte values, it will look something like this:
 arr[0][0] (0x6060) arr[0] (0x5050) points to (0x6060) arr[0][1] (0x6068) arr[0][2] (0x6070) arr[1][0] (0x8080) arr (0x4040) points to (0x5050) arr[1] (0x5058) points to (0x8080) arr[1][1] (0x8088) arr[1][2] (0x8090) arr[2][0] (0x2020) arr[2] (0x5060) points to (0x2020) arr[2][1] (0x2028) arr[2][2] (0x2030)

Only freeing arr will result in (the example above) 0x5050-0x5060 being freed, even though these memory addresses contain pointers to still-allocated memory​.

11. Main() should return an int.

You shouldn't cast the return value of malloc unless you need to use this code with a C++ compiler.

So, you do have an infinite loop in there >_<. Add this to the for loop at line 14 and run the code to see what happens:

Code:
``` for(r=0; columns; r++) {
arr[r]=(double*)malloc(columns*sizeof(double));
fprintf(stderr, "allocated row %d\n", r);
}```
And remember what I wrote in post #23 about freeing all the pointers in **arr, not just **arr itself.

12. Originally Posted by MK27
Main() should return an int.

You shouldn't cast the return value of malloc unless you need to use this code with a C++ compiler.

So, you do have an infinite loop in there >_<. Add this to the for loop at line 14 and run the code to see what happens:

Code:
``` for(r=0; columns; r++) {
arr[r]=(double*)malloc(columns*sizeof(double));
fprintf(stderr, "allocated row %d\n", r);
}```
And remember what I wrote in post #23 about freeing all the pointers in **arr, not just **arr itself.
Now i had changed my code from "malloc" to "calloc" because if i use malloc i don't get any output

Code:
Code:
```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
double** arr;
int rows = 1500;
int columns = 1500;
int m[1500];
int i,j,r;
i=0;
arr= (double **) calloc(rows,sizeof(double*));
for(r=0; columns; r++){
arr[r]=(double*)calloc(columns,sizeof(double));
fprintf(stderr, "allocated row %d\n", r);
}
// FREEING MEMORY
for (r = 0; r < columns; i++)    {
free(arr[r]);
}
srand((unsigned)time(NULL));
for(j=0; j<columns; j++){
m[j]= 0;
while(m[j] < 15){
i= (int) rand()%1500;
if (arr[i][j] == 0.0) {
arr[i][j]= (double) rand()/RAND_MAX;
if(arr[i][j] > 0){
m[j]++;
printf("arr[%d][%d] = %lf\t", i, j, arr[i][j]);
printf("\n");
}
}
}
}
free(arr);
getch();
}```
I got the following error:

And the output :
allocated row 11299
allocated row 11300
allocated row 11301
allocated row 11302
allocated row 11303
allocated row 11304
allocated row 11305
allocated row 11306
allocated row 11307
allocated row 11308
allocated row 11309
allocated row 11310
allocated row 11311
allocated row 11312
allocated row 11313
allocated row 11314
allocated row 11315
allocated row 11316
allocated row 11317
allocated row 11318
allocated row 11319
allocated row 11320
allocated row 11321
allocated row 11322
allocated row 11323
allocated row 11324
allocated row 11325
allocated row 11326
allocated row 11327
allocated row 11328
allocated row 11329
allocated row 11330
allocated row 11331
allocated row 11332
allocated row 11333
allocated row 11334
allocated row 11335
allocated row 11336
allocated row 11337
allocated row 11338
allocated row 11339
allocated row 11340
allocated row 11341
allocated row 11342
allocated row 11343
allocated row 11344
allocated row 11345
allocated row 11346
allocated row 11347
allocated row 11348
allocated row 11349
allocated row 11350
allocated row 11351
allocated row 11352
allocated row 11353
allocated row 11354
allocated row 11355
allocated row 11356
allocated row 11357
allocated row 11358
allocated row 11359
allocated row 11360
allocated row 11361
allocated row 11362
allocated row 11363
allocated row 11364
allocated row 11365
allocated row 11366
allocated row 11367
allocated row 11368
allocated row 11369
allocated row 11370
allocated row 11371
allocated row 11372
allocated row 11373
allocated row 11374
allocated row 11375
allocated row 11376
allocated row 11377
allocated row 11378
allocated row 11379
allocated row 11380
allocated row 11381
allocated row 11382
allocated row 11383
allocated row 11384
allocated row 11385
allocated row 11386
allocated row 11387
allocated row 11388
allocated row 11389
allocated row 11390
allocated row 11391
allocated row 11392
allocated row 11393
allocated row 11394
allocated row 11395
allocated row 11396
allocated row 11397
allocated row 11398
allocated row 11399
allocated row 11400
allocated row 11401
allocated row 11402
allocated row 11403
allocated row 11404
allocated row 11405
allocated row 11406
allocated row 11407
allocated row 11408
allocated row 11409
allocated row 11410
allocated row 11411
allocated row 11412
allocated row 11413
allocated row 11414
allocated row 11415
allocated row 11416
allocated row 11417
allocated row 11418
allocated row 11419
allocated row 11420
allocated row 11421
allocated row 11422
allocated row 11423
allocated row 11424
allocated row 11425
allocated row 11426
allocated row 11427
allocated row 11428
allocated row 11429
allocated row 11430
allocated row 11431
allocated row 11432
allocated row 11433
allocated row 11434
allocated row 11435
allocated row 11436
allocated row 11437
allocated row 11438
allocated row 11439
allocated row 11440
allocated row 11441
allocated row 11442
allocated row 11443
allocated row 11444
allocated row 11445
allocated row 11446
allocated row 11447
allocated row 11448
allocated row 11449
allocated row 11450
allocated row 11451
allocated row 11452
allocated row 11453
allocated row 11454
allocated row 11455
allocated row 11456
allocated row 11457
allocated row 11458
allocated row 11459
allocated row 11460
allocated row 11461
allocated row 11462
allocated row 11463
allocated row 11464
allocated row 11465
allocated row 11466
allocated row 11467
allocated row 11468
allocated row 11469
allocated row 11470
allocated row 11471
allocated row 11472
allocated row 11473
allocated row 11474
allocated row 11475
allocated row 11476
allocated row 11477
allocated row 11478
allocated row 11479
allocated row 11480
allocated row 11481
allocated row 11482
allocated row 11483
allocated row 11484
allocated row 11485
allocated row 11486
allocated row 11487
allocated row 11488
allocated row 11489
allocated row 11490
allocated row 11491
allocated row 11492
allocated row 11493
allocated row 11494
allocated row 11495
allocated row 11496
allocated row 11497
allocated row 11498
allocated row 11499
allocated row 11500
allocated row 11501
allocated row 11502
allocated row 11503
allocated row 11504
allocated row 11505
allocated row 11506
allocated row 11507
allocated row 11508
allocated row 11509
allocated row 11510
allocated row 11511
allocated row 11512
allocated row 11513
allocated row 11514
allocated row 11515
allocated row 11516
allocated row 11517
allocated row 11518
allocated row 11519
allocated row 11520
allocated row 11521
allocated row 11522
allocated row 11523
allocated row 11524
allocated row 11525
allocated row 11526
allocated row 11527
allocated row 11528
allocated row 11529
allocated row 11530
allocated row 11531
allocated row 11532
allocated row 11533
allocated row 11534
allocated row 11535
allocated row 11536
allocated row 11537
allocated row 11538
allocated row 11539
allocated row 11540
allocated row 11541
allocated row 11542
allocated row 11543
allocated row 11544
allocated row 11545
allocated row 11546
allocated row 11547
allocated row 11548
allocated row 11549
allocated row 11550
allocated row 11551
allocated row 11552
allocated row 11553
allocated row 11554
allocated row 11555
allocated row 11556
allocated row 11557
allocated row 11558
allocated row 11559
allocated row 11560
allocated row 11561
allocated row 11562
allocated row 11563
allocated row 11564
allocated row 11565
allocated row 11566
allocated row 11567
allocated row 11568
allocated row 11569
allocated row 11570
allocated row 11571
allocated row 11572
allocated row 11573
allocated row 11574
allocated row 11575
allocated row 11576
allocated row 11577
allocated row 11578
allocated row 11579
allocated row 11580
allocated row 11581
allocated row 11582
allocated row 11583
allocated row 11584
allocated row 11585
allocated row 11586
allocated row 11587
allocated row 11588
allocated row 11589
allocated row 11590
allocated row 11591
allocated row 11592
allocated row 11593
allocated row 11594
allocated row 11595
allocated row 11596
allocated row 11597

13. Use a little logic and think about what you wrote. What good does freeing the memory do before you can actually use it? Free everything at the end, where you freed the main array.

Did you not read my other post? Quit casting malloc! And if it gives you errors, compile as C (not c++).

Also note that the error might be because every time you run calloc() in the loop, you're allocating 12,000 bytes. Run this 1500 times, and you'll be allocating 18,000,000 bytes. Your OS probably doesn't allow a single program to take up this much memory.

14. You are getting good advice above.

Making the first malloc() into a calloc() is not necessary since immediately you initialize all of the elements of this dimension in the first for loop when you make the other calls malloc() or calloc().

The second call needs to be either a calloc() so that the individual review values are 0.0 or you need a separate loop to initialize all the reviews to 0.0 using a separate loop. I am assuming that the floating point on your implementation is one where 0.0 is all bits zero. I have not seen one where that is not true. But I am not going to take the time to check the standard to see if that is required of floating point values.

15. Originally Posted by pheininger
The second call needs to be either a calloc() so that the individual review values are 0.0 or you need a separate loop to initialize all the reviews to 0.0 using a separate loop. I am assuming that the floating point on your implementation is one where 0.0 is all bits zero. I have not seen one where that is not true. But I am not going to take the time to check the standard to see if that is required of floating point values.
Use a separate loop. calloc only gives all bits zero, and there is no guarantee that 0.0 is represented as all bits zero.