C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2013, 01:04 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
Generating number without repetition

assuming the function is generate key, I want to store the numbers in an array, the size of the array depends on a predefined variable "size", so here's the function


srand(time (NULL));
for (i = ; i < size; i++){
array[i] = rand()%size;
}

i tried putting if statements do another loop but it's just not working, anyone can chime in?

thanks in advance

Reply With Quote
  #2  
Old February 16th, 2013, 03:27 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 19 m 34 sec
Reputation Power: 1774
Start with this
for (i = 0 ; i < size; i++) array[i] = i;

Then shuffle the array:
Pick random x and y indices in the array, and swap the two elements.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 16th, 2013, 03:27 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
Maybe the following could help

http://stackoverflow.com/questions/5064379/generating-unique-random-numbers-in-c


Regards,
Dariyoosh

Reply With Quote
  #4  
Old February 16th, 2013, 03:30 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
sorry I should have mentioned i'd need the numbers to be shuffled that's why I used rand.

doing array[i] = i will just give me a list of numbers of ascending order not shuffled

and I don't want to get an ascending list and shuffle it, I want to use the rand command. I'm working on an assignment and I have to use it

Reply With Quote
  #5  
Old February 16th, 2013, 10:49 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Salem's method has the advantage that it cheaply (little resources) meets your headline requirement "Generating number without repetition".

Fisher-Yates shuffle
Quote:
Originally Posted by wikipedia
To shuffle an array a of n elements (indices 0..n-1):
Code:
  for i from n − 1 downto 1 do
       j ← random integer with 0 ≤ j ≤ i
       exchange a[j] and a[i]
Comments on this post
codergeek42 agrees: Particularly useful for the BogoSort algorithm
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #6  
Old February 16th, 2013, 12:12 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 37 sec
Reputation Power: 1800
Quote:
Originally Posted by maans88
sorry I should have mentioned i'd need the numbers to be shuffled that's why I used rand.
[...]
I don't want to get an ascending list and shuffle it, I want to use the rand command. I'm working on an assignment and I have to use it

Indeed the rand() function will be useful in shuffling.

It is inefficient to try this in one step as you suggest. After size - 1 numbers, there would only be one acceptable value remaining, and it would take rand() a non deterministic length of time to return it, given a sufficiently bad rand() implementation, and large valie of size it may never complete.

You might try the following algorithm, it is at least bounded in its execution time.

Code:
1) Create an array, initialise all values to -1 (indicating empty - use memset() for this) 
2) For each n from 0 to size -1...
3)   pick a random number i from 0 to size -1
4)   while array[n] != -1
5)      n = (n + 1) % size
7)   wend
8)   array[n] = i
9 endfor

That way deterministic values i are placed at random positions in array, using the -1 "empty" value to find the first empty position after the random index (with wrap-around). The worst case will be that you select a location one after the last available location so that the while loop wraps all the war around.


Note that selecting a random range using rand() % size is somewhat less than random since it introduces bias that can be significant for large values of size relative to RAND_MAX that are not an integer power of two. The following is a better solution:

Code:
i = (int)((double)rand() / (double)RAND_MAX) * size ;

Last edited by clifford : February 16th, 2013 at 12:24 PM.

Reply With Quote
  #7  
Old February 16th, 2013, 12:16 PM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
one of my friends did it using a while loop and and a for loop inside it.

the outter loop is the while loop and the inner is teh for loop

any idea?

Reply With Quote
  #8  
Old February 16th, 2013, 12:32 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 37 sec
Reputation Power: 1800
Quote:
Originally Posted by maans88
one of my friends did it using a while loop and and a for loop inside it.

the outter loop is the while loop and the inner is teh for loop

any idea?


If your friend does not think that it is fair to reveal his algorithm, then why would it be more fair for complete strangers to do so!? He's your friend - you ask him.

Pretty much any algorithm will involve nested loops one way or another so knowing merely that your friend used a for and a while in a particular order does little to reveal his solution. Moreover in C any while loop can be implemented as a for loop, and any for loop as a while loop, so the particular loop used reveals nothing. You could even implement a loop using goto (don't by the way), so it tells us nothing.

Besides I gave you a usable algorithm what's wrong with that? It would be far better for you to submit a solution that at least looked like you came up with it. Whatever you choose to do, do make sure that you understand the solution, you may be asked to explain it to your tutor.

Last edited by clifford : February 16th, 2013 at 02:20 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Generating number without repetition

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap