Thread: Random Number Generator

  1. #1
    Registered User Unee0x's Avatar
    Join Date
    Nov 2011
    Posts
    12

    Random Number Generator

    I am trying to generate 10 random numbers from 0-9, which goes well but I'd like them to each be unique, meaning for each one to only be used once.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main()
    {
      srand(time(NULL));
      char Array[10];
      int i;
      for(i=0;i<10;i++)
        {
          sprintf(Array,"%d",rand()%9+1);
          printf("%s\n",Array);
        }
      return 0;
    }
    How can I get this function to avoid repeats.
    I would just like to be pointed in the right direction..

    Thank you in advance...

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use a shuffling algorithm. Plenty of information around if you google for "random unique shuffle"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you want them to be from 0 to 9, why are you generating numbers from 1 to 9?
    You could just take the remainder of dividing by 10, instead of the remainder of divising by 9 and adding 1.

    But anyway, yes you should just fill the array with the numbers 0 to 9 and then shuffle them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Yes, that's the way.
    A lot of people get shuffling wrong, because they go through the array, swapping every ith element with one in a random position. This doesn't produce and even distribution of permutations (try it and print out the results). You need to go through, swapping with an element in the range i-
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. Random number generator
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 02:23 PM
  4. Random number generator between 0 and 1
    By Lord CyKill in forum C# Programming
    Replies: 1
    Last Post: 03-29-2007, 08:03 AM
  5. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM