Click here to Skip to main content
15,900,816 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionAnother random number generator Pin
ChandraRam25-Nov-07 21:40
ChandraRam25-Nov-07 21:40 
AnswerRe: Another random number generator Pin
CPallini25-Nov-07 22:27
mveCPallini25-Nov-07 22:27 
GeneralRe: Another random number generator Pin
ChandraRam25-Nov-07 22:31
ChandraRam25-Nov-07 22:31 
GeneralRe: Another random number generator Pin
CPallini25-Nov-07 22:39
mveCPallini25-Nov-07 22:39 
GeneralRe: Another random number generator Pin
ChandraRam25-Nov-07 22:46
ChandraRam25-Nov-07 22:46 
GeneralRe: Another random number generator Pin
CPallini25-Nov-07 23:05
mveCPallini25-Nov-07 23:05 
GeneralRe: Another random number generator Pin
ChandraRam25-Nov-07 23:10
ChandraRam25-Nov-07 23:10 
GeneralRe: Another random number generator Pin
Ian Shlasko30-Nov-07 11:48
Ian Shlasko30-Nov-07 11:48 
I think Pallini actually has your answer. Think of it on a smaller scale at first...

With the numbers you gave, you just satisfied the first condition (Set of 10, ending in each digit from 0 to 9). Of course, just the numbers 0 to 9 would satisfy that condition.

Now move to the 100s... You basically need 100 numbers that end in 00-99, and 00, 01, 02, ... 98, 99 satisfies that condition. Move to the thousands, and the ten thousands, and there you are.

So technically, a set of 00000-99999 in order satisfies every condition except the randomness. If you want bigger numbers, you can just prepend a few random digits to each, the same way you use "21890" instead of just "0" in your example. Since the last N digits are unique, the number as a whole is unique.

So if I'm right so far, I have your answer.

Create an array of numbers from 0 to 99999 (Maximum - 1) such that Array[N] = N. Just to start off.

Start by randomly sorting each block of 10. First randomize elements 0-9, them 10-19, and so on.

Then sort the blocks themselves. Treat 0-9 as a cohesive unit, 10-19 as a unit, and so on. Sort each block of 100 as if it was comprised of ten blocks.

As you can probably see by now, you just move up one power of ten at a time. Next you sort each block of 1000, treating it as 10 blocks of 100 each, then the whole array as 10 blocks of 1000 each.

You could even do this backwards, first sorting the big blocks, then the little blocks.

At this point, the entire array is randomized, yet will still satisfy your conditions.

This will of course take quite a while if you use a larger set, but there are ways around that too. Use a tree instead. Build a tree where each node has ten children (Except the leaf nodes). These children start out as "0", "1", ..., "8", "9". Traverse the whole tree, level by level, and shuffle the children of each node. Then just traverse the tree to grab all of the shuffled numbers in order, and pipe them into an array. Someone elsewhere in the thread showed you how to quickly search for them.

Ok, this post is way longer than I intended, and I was going to leave the office over a half hour ago, so good luck Smile | :)
GeneralRe: Another random number generator Pin
ChandraRam1-Dec-07 7:32
ChandraRam1-Dec-07 7:32 
AnswerRe: Another random number generator Pin
cp987625-Nov-07 23:44
cp987625-Nov-07 23:44 
GeneralRe: Another random number generator Pin
ChandraRam25-Nov-07 23:54
ChandraRam25-Nov-07 23:54 
GeneralRe: Another random number generator Pin
cp987626-Nov-07 0:42
cp987626-Nov-07 0:42 
GeneralRe: Another random number generator Pin
ChandraRam26-Nov-07 0:46
ChandraRam26-Nov-07 0:46 
GeneralRe: Another random number generator Pin
cp987626-Nov-07 0:51
cp987626-Nov-07 0:51 
GeneralRe: Another random number generator Pin
ChandraRam26-Nov-07 0:54
ChandraRam26-Nov-07 0:54 
GeneralRe: Another random number generator Pin
Mark Churchill19-Dec-07 15:10
Mark Churchill19-Dec-07 15:10 
GeneralRe: Another random number generator Pin
ChandraRam19-Dec-07 18:58
ChandraRam19-Dec-07 18:58 
QuestionEM algorithm Pin
yanhe011615-Nov-07 12:11
yanhe011615-Nov-07 12:11 
AnswerRe: EM algorithm Pin
Luc Pattyn15-Nov-07 12:19
sitebuilderLuc Pattyn15-Nov-07 12:19 
AnswerRe: EM algorithm Pin
El Corazon20-Nov-07 7:35
El Corazon20-Nov-07 7:35 
QuestionHow is BitBlt implemented [modified] Pin
Force Code15-Nov-07 8:15
Force Code15-Nov-07 8:15 
AnswerRe: How is BitBlt implemented Pin
Luc Pattyn15-Nov-07 8:27
sitebuilderLuc Pattyn15-Nov-07 8:27 
GeneralRe: How is BitBlt implemented Pin
Force Code15-Nov-07 8:43
Force Code15-Nov-07 8:43 
GeneralRe: How is BitBlt implemented Pin
Luc Pattyn15-Nov-07 9:22
sitebuilderLuc Pattyn15-Nov-07 9:22 
GeneralRe: How is BitBlt implemented Pin
Force Code15-Nov-07 9:54
Force Code15-Nov-07 9:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.