![]() |
|
#1
|
|||
|
|||
Generating Random # in Array without repeatshey guys I'm having a problem generating a random number in an array without any repeats. I believe the problem is within my inner for loop but I'm not sure exactly what it is.
CPP / C++ / C Code:
i think the problem is that the inner loop re randoms the number the first time then increments... im not sure how to fix this... if anyone could help would be very appreciated... thanks. Last edited by admin : 01-Mar-2009 at 20:26.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Generating Random # in Array without repeatsQuote:
Having said that, it appears that you have an array of five elements of which you want to randomly assign the values 0-4. The point of the inner loop is to ensure that whatever random number is chosen, it has not been previously assigned to an earlier element. You have the beginnings of implementing this algorithm, & you are correct that it is not complete, so the next step is to devise a strategy to arrive at a workable solution. With pencil & paper, how would you determine whether any random number has previously been assigned? There are two choices:
|
|
#3
|
|||
|
|||
Re: Generating Random # in Array without repeatsI understand that:
i set a random value to a[0] so a[0] = 1; then i check a[0] with a[1]; if a[0] == a[1] random a new number until a[0] != a[1] then move onto a[1] check a[1] with a[0] if a[1] == a[0] random a new number until a[1] != a[0] i guess the part that I'm not getting is the code on how to check the previous value without checking the value of the current value... such has I check a[1] with a[0]... then i check a[1] with a[1]... how do i avoid checking a[1] with a[1] this is the unsuccessful code I've come up with again. CPP / C++ / C Code:
Last edited by admin : 03-Mar-2009 at 00:53.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: Generating Random # in Array without repeatsQuote:
You need a loop with another variable. For each value of i, this new variable starts at zero and goes up through the previously assigned values. If it gets all of the way up through i-1 without finding that the current random value has already been assigned, then you can use the current random value, otherwise you have to try again. I think you could try something like the following semi-pseudo code. See Footnote. Given an array with num elements: We want to assign values 0, 1, ..., num-1 "randomly" to the members of the array, with no repeated values. Here is a naive method ("brute force"): Code:
I think that this is what you are trying to get at. Look at LOOP3: It investigates only the values of array members that have already been assigned. There are other ways of doing the deed that have considerable advantages, especially for large data sets. Specifically: Initialize the array sequentially, then perform a random shuffle. (Look it up if you are interested. The code is actually simpler than this, but I think it's important to understand how to do it the way that you started.) Regards, Dave Footnote: I call it semi-pseudo code because, unlike true pseudo code, it has some language-specific constructs. Every time someone talks about pseudo code, I get a flashback to: "I may be pseudo-intellectual, but at least I'm not quasi." ---William F. Buckley Responding to some non-constructive criticism about his conservative views way back in the mid-20th century. |
|
#5
|
|||
|
|||
Re: Generating Random # in Array without repeatsI've tried the above code and I'm still getting duplicate numbers...
CPP / C++ / C Code:
am i sitll missing part of the code? Last edited by LuciWiz : 10-Mar-2009 at 02:10.
Reason: Please insert your C++ between [cpp] & ]/cpp] tags
|
|
#6
|
|||
|
|||
Re: Generating Random # in Array without repeatsstd::unique() may help.
I will random each number in an array and check using unique. For example, 0, 1, 2, 3, 4 -- 5; loop == 1 then i check with unique loop == 2 then i check with unique if not unique rerand() with another seed value; or if you don't want to use unique(), then you have check one by one from start of array until the element being rand(). Hope helps. |
|
#7
|
|||
|
|||
Re: Generating Random # in Array without repeatsQuote:
1. You didn't implement my pseudo code, and I put comments in to show what's wrong. 2. Instrument your code to make it tell you exactly what it is printing. Before changing anything else, change your print statement to be more verbose. Put in another print statement in the block that I objected to in my comments. Maybe something like CPP / C++ / C Code:
3. What is the value of ARRAY_SIZE? If it is greater than 10, the corrected code will loop infinitely, since your code can generate at most ten different numbers. 0, 1, 2, 3, 4, 5, 6, 7, 8. 9. 4. You are printing out all values that you get from rand(), including duplicates. Try ARRAY_SIZE = 10 (or smaller), and print out that many array values after you leave the the "while(..." loop. Regards, Dave Last edited by davekw7x : 10-Mar-2009 at 09:15.
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 16:08 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 17:16 |
| Returning a 2 dimensional Array from a function | vicky_brsh | C++ Forum | 1 | 04-Jan-2008 15:06 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| generating arrays from main multi-dimensional array | raisinlove | MySQL / PHP Forum | 1 | 25-Jul-2005 13:29 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The