Random numbers that do not match each other


I want to produce different numbers with C. We can generate a random number using the stdlib library and the srand function.

For example; I want to produce a random number between 0 and 5.

#include <stdio.h> #include <time.h> #include <stdlib.h> int main(void) { int i; int n = 4; int array[3]; srand(time(NULL)); for(i = 0; i < n; i++) { array[i] = rand() % 5; printf("%d\n", array[i]); } return 0; <hr />

But the same numbers may coincide here.Like this:

2 4 4 1

How can I prevent this?


Maybe you can use something like this:

#include <stdio.h> #include <time.h> #include <stdlib.h> int main(void) { int i; int n = 4; int array[4]; // Fill an array with possible values int values[5] = {0, 1, 2, 3, 4}; srand(time(NULL)); for(i = 0; i < n; i++) { int t1 = rand() % (5-i); // Generate next index while making the // possible value one lesser for each // loop array[i] = values[t1]; // Assign value printf("%d\n", array[i]); values[t1] = values[4-i]; // Get rid of the used value by // replacing it with an unused value } return 0; }


Instead of random number you can generate random non-zero shift from the previous number:

#include <stdio.h> #include <stdlib.h> int myrand() { static int prev = -1; if (prev < 0) prev = rand() % 5; prev = (prev + 1 + rand() % 4) % 5; return prev; } int main(void) { int i; for (i = 0; i < 20; i++) printf("%d\n", myrand()); }


  • How do I stop js files being cached in IE?
  • How to generate random events in android?
  • fscanf not reading floats correctly
  • Yii: any way to save the images in compressed form?
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • Inversing an interpolation of rotation
  • Detecting null parameter in preprocessor macro
  • Date difference with leap year
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • embed rChart in Markdown
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?