| | |
not getting non repeating random numbers many times
![]() |
•
•
Join Date: Jul 2005
Posts: 4
Reputation:
Solved Threads: 0
hi
i want to generate non repeated random numbers many times. for that i m using the following code.
<< moderator edit: added [code][/code] tags >>
it generates random numbers from 0 to 20 without repeating in an array. but it generates only once. next time in the for loop ( in j ) it gives the same pattern again , which i dont want. can some body please help me to get different pattern every time in the for loop ( for j ).
i want to generate non repeated random numbers many times. for that i m using the following code.
C Syntax (Toggle Plain Text)
#include "sys/types.h" #include "stdio.h" #define MAX 200 #define N 20 main() { int array[N],r; int n = 0; int count_check,count_gen ,i,j; for(j=0;j<10;j++,printf("\n")) { srand(j); for (count_gen=0;count_gen<=MAX;count_gen++) { r = rand()%N; for ( count_check = 0; count_check < n; count_check++ ) { if ( r == array[count_check] )break; }// end for count_check. if ( count_check == n ) array[n++] = r; }// end for count_gen. for(i=0;i<N;i++) printf("%d\n",array[i]); }// end for j. }// end of main.
it generates random numbers from 0 to 20 without repeating in an array. but it generates only once. next time in the for loop ( in j ) it gives the same pattern again , which i dont want. can some body please help me to get different pattern every time in the for loop ( for j ).
Call srand once in a program, before the loop.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Read this on random numbers http://www.eternallyconfuzzled.com/tuts/random.html
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
•
•
•
•
Originally Posted by xshashiy
I have tried that but it does not work. it produces same pattern of numbers.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Random Numbers on button (Java)
- C++ Random Numbers (C++)
- Program will not generate random numbers (C++)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
- random numbers all different??? (C++)
Other Threads in the C Forum
- Previous Thread: recursion and for loop to recursion
- Next Thread: Sane way of dynamically allocating arrays beyond declaration.
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






