User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,018 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2406 | Replies: 4
Reply
Join Date: Jul 2005
Posts: 4
Reputation: xshashiy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xshashiy xshashiy is offline Offline
Newbie Poster

not getting non repeating random numbers many times

  #1  
Jul 6th, 2005
hi
i want to generate non repeated random numbers many times. for that i m using the following code.

#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.
<< 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 ).
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: not getting non repeating random numbers many times

  #2  
Jul 6th, 2005
Call srand once in a program, before the loop.
Reply With Quote  
Join Date: Jul 2005
Posts: 4
Reputation: xshashiy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xshashiy xshashiy is offline Offline
Newbie Poster

Re: not getting non repeating random numbers many times

  #3  
Jul 7th, 2005
Originally Posted by Dave Sinkula
Call srand once in a program, before the loop.


I have tried that but it does not work. it produces same pattern of numbers.


regards
shashi
Reply With Quote  
Join Date: Nov 2004
Location: Tucson, Az
Posts: 107
Reputation: prog-bman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
prog-bman prog-bman is offline Offline
Junior Poster

Re: not getting non repeating random numbers many times

  #4  
Jul 7th, 2005
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: not getting non repeating random numbers many times

  #5  
Jul 7th, 2005
Originally Posted by xshashiy
I have tried that but it does not work. it produces same pattern of numbers.
Using the same seed will produce the same pattern. Often srand(time(NULL)); is used, but read the link prog-bman posted.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC