Home:ALL Converter>Generate random numbers without repeats

Generate random numbers without repeats

Ask Time:2015-02-17T12:37:05         Author:Dilini Himali

Json Formatter

I want to generate random numbers without repeats till all gone, then again generating random numbers with the initial dataset.

I know keeping already generated numbers in an array and loopin through them to check whether it is alredy generated or the method deducting the numbers that are generated from the array and randomize numbers with the new array.

What I want is not those methods, if there is a way that is efficient using data structures will be quite nice, if it is any other method also ok

Thanks

Author:Dilini Himali,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28554706/generate-random-numbers-without-repeats
Jim Mischel :

Say you want to generate 1,000 unique random numbers and present them to some code one at a time. When you exhaust those numbers, you want to present the same numbers again, but in a different sequence.\n\nTo generate the numbers, use a hash table. In C#, it would look like this:\n\nconst int MaxNumbers = 1000;\nHashSet<int> uniqueNumbers = new HashSet<int>();\nRandom rnd = new Random();\n// generate random numbers until there are 1000 in the hash table\nwhile (uniqueNumbers.Count < MaxNumbers)\n{\n uniqueNumbers.Add(rnd.Next());\n}\n// At this point you have 1,000 unique random numbers\n// Put them in an array\nint[] myNumbers = uniqueNumbers.ToArray();\n\n\nThis works because the HashSet.Add method rejects duplicates. And it's very fast because lookup in the hash table is O(1).\n\nNow, you can serve them by setting a current index at 0 and increment it every time a number is requested. Something like:\n\nint ixCurrent = 0;\nint GetNextNumber()\n{\n if (ixCurrent < MaxNumbers)\n {\n ++ixCurrent;\n return myNumbers[ixCurrent-1];\n }\n\n\nBut what to do when ixCurrent runs off the end of the array? Enter the Fisher-Yates Shuffle: \n\n // out of numbers. Shuffle the array and return the first one.\n for (int i = MaxNumbers-1; i > 0; --i)\n {\n int j = rnd.Next(i+1);\n int temp = myNumbers[i];\n myNumbers[i] = myNumbers[j];\n myNumbers[j] = temp;\n }\n ixCurrent = 1;\n return myNumbers[0];\n}\n\n\nIf you know that the numbers you want to return are within a particular range (that is, you want to return the numbers 0-999 in random order), then you just fill an array with the values 0-999 and shuffle it.",
2015-02-17T14:26:28
user4064342 :

I'm not sure what language you are using, but here's some C++ code that does what you're looking for. Instead of it searching an array, it just does a direct check of a specific section of memory for a set flag and if it isn't set then the number chosen is new and printed.\n\nThe section I marked as handler is the code that is first executed when a unique number is found. Change the 10's and the 11 to different numbers if you want a larger set of random numbers but you might have to wait forever for the output.\n\nint main(int argc, char *argv[]){\nchar randn[10];\nchar randnset[10];\nint n;\nint ct=0;\nmemset(randnset,'1',10);\nmemset(randn,0,10);\nwhile (ct < 10){\n srand(time(NULL));\n n=rand() % 11;\n if (!randn[n]){\n printf(\"%d\\n\",n); // handler\n randn[n]='1';\n ct++;\n }\n}\nreturn 0;\n}\n",
2015-02-17T05:01:02
yy