Random Number Generator

This is a discussion on Random Number Generator within the C Programming forums, part of the General Programming Boards category; I am trying to generate 10 random numbers from 0-9, which goes well but I'd like them to each be ...

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

    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.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
      char Array[10];
      int i;
      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
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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
    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.

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, 07:48 AM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 08:26 AM
  3. Random number generator
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 03:23 PM
  4. Random number generator between 0 and 1
    By Lord CyKill in forum C# Programming
    Replies: 1
    Last Post: 03-29-2007, 09:03 AM
  5. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21