How do I generate random numbers without rand() function?












23















I want to generate (pseudo) random numbers between 0 and some integer. I don't mind if they aren't too random. I have access to the current time of the day but not the rand function. Can anyone think of a sufficiently robust way to generate these? Perhaps, discarding some bits from time of day and taking modulo my integer or something?



I am using c.










share|improve this question




















  • 2





    This sounds like homework. If it is, you should tag it with the "homework" tag.

    – jadarnel27
    Sep 29 '11 at 20:19













  • If you have access to google.com, try searching for this: "random number generator".

    – DwB
    Sep 29 '11 at 20:19






  • 2





    Why not simply read from /dev/random? Or use the xkcd method.

    – user142019
    Sep 29 '11 at 20:21








  • 2





    What's preventing you from simply using random() then?

    – Staven
    Sep 29 '11 at 20:24






  • 1





    rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

    – SoapBox
    Sep 29 '11 at 20:40
















23















I want to generate (pseudo) random numbers between 0 and some integer. I don't mind if they aren't too random. I have access to the current time of the day but not the rand function. Can anyone think of a sufficiently robust way to generate these? Perhaps, discarding some bits from time of day and taking modulo my integer or something?



I am using c.










share|improve this question




















  • 2





    This sounds like homework. If it is, you should tag it with the "homework" tag.

    – jadarnel27
    Sep 29 '11 at 20:19













  • If you have access to google.com, try searching for this: "random number generator".

    – DwB
    Sep 29 '11 at 20:19






  • 2





    Why not simply read from /dev/random? Or use the xkcd method.

    – user142019
    Sep 29 '11 at 20:21








  • 2





    What's preventing you from simply using random() then?

    – Staven
    Sep 29 '11 at 20:24






  • 1





    rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

    – SoapBox
    Sep 29 '11 at 20:40














23












23








23


13






I want to generate (pseudo) random numbers between 0 and some integer. I don't mind if they aren't too random. I have access to the current time of the day but not the rand function. Can anyone think of a sufficiently robust way to generate these? Perhaps, discarding some bits from time of day and taking modulo my integer or something?



I am using c.










share|improve this question
















I want to generate (pseudo) random numbers between 0 and some integer. I don't mind if they aren't too random. I have access to the current time of the day but not the rand function. Can anyone think of a sufficiently robust way to generate these? Perhaps, discarding some bits from time of day and taking modulo my integer or something?



I am using c.







c random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 29 '11 at 20:18









Bill Lynch

60.3k1197142




60.3k1197142










asked Sep 29 '11 at 20:17









AnkurVjAnkurVj

3,10383047




3,10383047








  • 2





    This sounds like homework. If it is, you should tag it with the "homework" tag.

    – jadarnel27
    Sep 29 '11 at 20:19













  • If you have access to google.com, try searching for this: "random number generator".

    – DwB
    Sep 29 '11 at 20:19






  • 2





    Why not simply read from /dev/random? Or use the xkcd method.

    – user142019
    Sep 29 '11 at 20:21








  • 2





    What's preventing you from simply using random() then?

    – Staven
    Sep 29 '11 at 20:24






  • 1





    rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

    – SoapBox
    Sep 29 '11 at 20:40














  • 2





    This sounds like homework. If it is, you should tag it with the "homework" tag.

    – jadarnel27
    Sep 29 '11 at 20:19













  • If you have access to google.com, try searching for this: "random number generator".

    – DwB
    Sep 29 '11 at 20:19






  • 2





    Why not simply read from /dev/random? Or use the xkcd method.

    – user142019
    Sep 29 '11 at 20:21








  • 2





    What's preventing you from simply using random() then?

    – Staven
    Sep 29 '11 at 20:24






  • 1





    rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

    – SoapBox
    Sep 29 '11 at 20:40








2




2





This sounds like homework. If it is, you should tag it with the "homework" tag.

– jadarnel27
Sep 29 '11 at 20:19







This sounds like homework. If it is, you should tag it with the "homework" tag.

– jadarnel27
Sep 29 '11 at 20:19















If you have access to google.com, try searching for this: "random number generator".

– DwB
Sep 29 '11 at 20:19





If you have access to google.com, try searching for this: "random number generator".

– DwB
Sep 29 '11 at 20:19




2




2





Why not simply read from /dev/random? Or use the xkcd method.

– user142019
Sep 29 '11 at 20:21







Why not simply read from /dev/random? Or use the xkcd method.

– user142019
Sep 29 '11 at 20:21






2




2





What's preventing you from simply using random() then?

– Staven
Sep 29 '11 at 20:24





What's preventing you from simply using random() then?

– Staven
Sep 29 '11 at 20:24




1




1





rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

– SoapBox
Sep 29 '11 at 20:40





rand() is typically implemented very simply (using a simple multiplication of the seed and then a mix)... its usually about one line. Just google it.

– SoapBox
Sep 29 '11 at 20:40












9 Answers
9






active

oldest

votes


















27














If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.



The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)



  unsigned short lfsr = 0xACE1u;
unsigned bit;

unsigned rand()
{
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
return lfsr = (lfsr >> 1) | (bit << 15);
}





share|improve this answer
























  • Exactly what I needed ! a very simple and elegant solution

    – AnkurVj
    Sep 29 '11 at 21:51



















10














For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.



This is called a linear congruential generator.



The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...






share|improve this answer

































    7














    Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.






    share|improve this answer































      0














      The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator






      share|improve this answer































        0














        You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html



        it is pure c and simple to use. E.g. just using time:



        #include "tinymt32.h"
        // And if you can't link:
        #include "tinymt32.c"

        #include <time.h>
        #include <stdio.h>

        int main(int argc, const char* argv)
        {
        tinymt32_t state;
        uint32_t seed = time(0);

        tinymt32_init(&state, seed);

        for (int i=0; i<10; i++)
        printf("random number %d: %un", i, (unsigned int)tinymt32_generate_uint32(&state));
        }





        share|improve this answer


























        • I can't use any additional libraries !

          – AnkurVj
          Sep 29 '11 at 21:07













        • What to you mean with can't? how about including another #include "tinymt32.c" ?

          – Dominic
          Sep 30 '11 at 19:45





















        0














        The smallest and simple random generator which work with ranges is provided below with fully working example.



        unsigned int MyRand(unsigned int start_range,unsigned int end_range)
        {
        static unsigned int rand = 0xACE1U; /* Any nonzero start state will work. */

        /*check for valid range.*/
        if(start_range == end_range) {
        return start_range;
        }

        /*get the random in end-range.*/
        rand += 0x3AD;
        rand %= end_range;

        /*get the random in start-range.*/
        while(rand < start_range){
        rand = rand + end_range - start_range;
        }

        return rand;
        }

        int main(void)
        {
        int i;
        for (i = 0; i < 0xFF; i++)
        {
        printf("%ut",MyRand(10,20));
        }
        return 0;
        }





        share|improve this answer































          0














          If you're not generating your numbers too fast (*1) and your upper limit is low enough (*2) and your "time of day" includes nanoseconds, just use those nanoseconds.



          #include <stdio.h>
          #include <stdlib.h>
          #include <time.h>

          int nanorand(void) {
          struct timespec p[1];
          clock_gettime(CLOCK_MONOTONIC, p);
          return p->tv_nsec % 1000;
          }

          int main(void) {
          int r, x;
          for (;;) {
          r = nanorand();
          do {
          printf("please type %d (< 50 quits): ", r);
          fflush(stdout);
          if (scanf("%d", &x) != 1) exit(EXIT_FAILURE);
          } while (x != r);
          if (r < 50) break;
          }
          puts("");
          return 0;
          }


          And a sample run ...




          please type 769 (< 50 quits): 769
          please type 185 (< 50 quits): 185
          please type 44 (< 50 quits): 44


          (*1) if you're using them interactively, one at a time

          (*2) if you want numbers up to about 1000






          share|improve this answer

































            -2














            #include<stdio.h>
            #include<conio.h>
            #include<stdlib.h>
            int main()
            {
            unsigned int x,r,i;
            // no of random no you want to generate
            scanf("%d",&x);
            // put the range of random no
            scanf("%d",&r);
            unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
            for(i=0;i<x;i++)
            printf("%d ",(a[i]%r)+1);
            free(a);
            getch();
            return 0;
            }





            share|improve this answer
























            • Can you bring an explanation for you code please! And Please read about how to write a good answer

              – eliasah
              Aug 16 '15 at 8:05











            • please stop using deprecated header files.

              – HaSeeB MiR
              Sep 22 '18 at 9:15



















            -2














            One of the simplest random number generator which not return allways the same value:



            uint16_t simpleRand(void)
            {
            static uint16_t r = 5531; //dont realy care about start value
            r+=941; //this value must be relative prime to 2^16, so we use all values
            return r;
            }


            You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.






            share|improve this answer
























            • Can someone explain the down vote?

              – 12431234123412341234123
              Dec 11 '17 at 11:39











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7602919%2fhow-do-i-generate-random-numbers-without-rand-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            27














            If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.



            The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)



              unsigned short lfsr = 0xACE1u;
            unsigned bit;

            unsigned rand()
            {
            bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
            return lfsr = (lfsr >> 1) | (bit << 15);
            }





            share|improve this answer
























            • Exactly what I needed ! a very simple and elegant solution

              – AnkurVj
              Sep 29 '11 at 21:51
















            27














            If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.



            The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)



              unsigned short lfsr = 0xACE1u;
            unsigned bit;

            unsigned rand()
            {
            bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
            return lfsr = (lfsr >> 1) | (bit << 15);
            }





            share|improve this answer
























            • Exactly what I needed ! a very simple and elegant solution

              – AnkurVj
              Sep 29 '11 at 21:51














            27












            27








            27







            If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.



            The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)



              unsigned short lfsr = 0xACE1u;
            unsigned bit;

            unsigned rand()
            {
            bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
            return lfsr = (lfsr >> 1) | (bit << 15);
            }





            share|improve this answer













            If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.



            The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)



              unsigned short lfsr = 0xACE1u;
            unsigned bit;

            unsigned rand()
            {
            bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
            return lfsr = (lfsr >> 1) | (bit << 15);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 29 '11 at 21:38









            RoddyRoddy

            46k35146240




            46k35146240













            • Exactly what I needed ! a very simple and elegant solution

              – AnkurVj
              Sep 29 '11 at 21:51



















            • Exactly what I needed ! a very simple and elegant solution

              – AnkurVj
              Sep 29 '11 at 21:51

















            Exactly what I needed ! a very simple and elegant solution

            – AnkurVj
            Sep 29 '11 at 21:51





            Exactly what I needed ! a very simple and elegant solution

            – AnkurVj
            Sep 29 '11 at 21:51













            10














            For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.



            This is called a linear congruential generator.



            The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...






            share|improve this answer






























              10














              For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.



              This is called a linear congruential generator.



              The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...






              share|improve this answer




























                10












                10








                10







                For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.



                This is called a linear congruential generator.



                The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...






                share|improve this answer















                For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.



                This is called a linear congruential generator.



                The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 8 '14 at 13:58









                user4422

                14918




                14918










                answered Sep 30 '11 at 1:39









                DennisDennis

                11.1k23554




                11.1k23554























                    7














                    Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.






                    share|improve this answer




























                      7














                      Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.






                      share|improve this answer


























                        7












                        7








                        7







                        Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.






                        share|improve this answer













                        Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Sep 29 '11 at 20:19









                        unwindunwind

                        322k52396527




                        322k52396527























                            0














                            The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator






                            share|improve this answer




























                              0














                              The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator






                              share|improve this answer


























                                0












                                0








                                0







                                The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator






                                share|improve this answer













                                The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Sep 29 '11 at 20:21









                                StavenStaven

                                2,650919




                                2,650919























                                    0














                                    You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html



                                    it is pure c and simple to use. E.g. just using time:



                                    #include "tinymt32.h"
                                    // And if you can't link:
                                    #include "tinymt32.c"

                                    #include <time.h>
                                    #include <stdio.h>

                                    int main(int argc, const char* argv)
                                    {
                                    tinymt32_t state;
                                    uint32_t seed = time(0);

                                    tinymt32_init(&state, seed);

                                    for (int i=0; i<10; i++)
                                    printf("random number %d: %un", i, (unsigned int)tinymt32_generate_uint32(&state));
                                    }





                                    share|improve this answer


























                                    • I can't use any additional libraries !

                                      – AnkurVj
                                      Sep 29 '11 at 21:07













                                    • What to you mean with can't? how about including another #include "tinymt32.c" ?

                                      – Dominic
                                      Sep 30 '11 at 19:45


















                                    0














                                    You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html



                                    it is pure c and simple to use. E.g. just using time:



                                    #include "tinymt32.h"
                                    // And if you can't link:
                                    #include "tinymt32.c"

                                    #include <time.h>
                                    #include <stdio.h>

                                    int main(int argc, const char* argv)
                                    {
                                    tinymt32_t state;
                                    uint32_t seed = time(0);

                                    tinymt32_init(&state, seed);

                                    for (int i=0; i<10; i++)
                                    printf("random number %d: %un", i, (unsigned int)tinymt32_generate_uint32(&state));
                                    }





                                    share|improve this answer


























                                    • I can't use any additional libraries !

                                      – AnkurVj
                                      Sep 29 '11 at 21:07













                                    • What to you mean with can't? how about including another #include "tinymt32.c" ?

                                      – Dominic
                                      Sep 30 '11 at 19:45
















                                    0












                                    0








                                    0







                                    You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html



                                    it is pure c and simple to use. E.g. just using time:



                                    #include "tinymt32.h"
                                    // And if you can't link:
                                    #include "tinymt32.c"

                                    #include <time.h>
                                    #include <stdio.h>

                                    int main(int argc, const char* argv)
                                    {
                                    tinymt32_t state;
                                    uint32_t seed = time(0);

                                    tinymt32_init(&state, seed);

                                    for (int i=0; i<10; i++)
                                    printf("random number %d: %un", i, (unsigned int)tinymt32_generate_uint32(&state));
                                    }





                                    share|improve this answer















                                    You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html



                                    it is pure c and simple to use. E.g. just using time:



                                    #include "tinymt32.h"
                                    // And if you can't link:
                                    #include "tinymt32.c"

                                    #include <time.h>
                                    #include <stdio.h>

                                    int main(int argc, const char* argv)
                                    {
                                    tinymt32_t state;
                                    uint32_t seed = time(0);

                                    tinymt32_init(&state, seed);

                                    for (int i=0; i<10; i++)
                                    printf("random number %d: %un", i, (unsigned int)tinymt32_generate_uint32(&state));
                                    }






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 30 '11 at 19:48

























                                    answered Sep 29 '11 at 20:40









                                    DominicDominic

                                    51644




                                    51644













                                    • I can't use any additional libraries !

                                      – AnkurVj
                                      Sep 29 '11 at 21:07













                                    • What to you mean with can't? how about including another #include "tinymt32.c" ?

                                      – Dominic
                                      Sep 30 '11 at 19:45





















                                    • I can't use any additional libraries !

                                      – AnkurVj
                                      Sep 29 '11 at 21:07













                                    • What to you mean with can't? how about including another #include "tinymt32.c" ?

                                      – Dominic
                                      Sep 30 '11 at 19:45



















                                    I can't use any additional libraries !

                                    – AnkurVj
                                    Sep 29 '11 at 21:07







                                    I can't use any additional libraries !

                                    – AnkurVj
                                    Sep 29 '11 at 21:07















                                    What to you mean with can't? how about including another #include "tinymt32.c" ?

                                    – Dominic
                                    Sep 30 '11 at 19:45







                                    What to you mean with can't? how about including another #include "tinymt32.c" ?

                                    – Dominic
                                    Sep 30 '11 at 19:45













                                    0














                                    The smallest and simple random generator which work with ranges is provided below with fully working example.



                                    unsigned int MyRand(unsigned int start_range,unsigned int end_range)
                                    {
                                    static unsigned int rand = 0xACE1U; /* Any nonzero start state will work. */

                                    /*check for valid range.*/
                                    if(start_range == end_range) {
                                    return start_range;
                                    }

                                    /*get the random in end-range.*/
                                    rand += 0x3AD;
                                    rand %= end_range;

                                    /*get the random in start-range.*/
                                    while(rand < start_range){
                                    rand = rand + end_range - start_range;
                                    }

                                    return rand;
                                    }

                                    int main(void)
                                    {
                                    int i;
                                    for (i = 0; i < 0xFF; i++)
                                    {
                                    printf("%ut",MyRand(10,20));
                                    }
                                    return 0;
                                    }





                                    share|improve this answer




























                                      0














                                      The smallest and simple random generator which work with ranges is provided below with fully working example.



                                      unsigned int MyRand(unsigned int start_range,unsigned int end_range)
                                      {
                                      static unsigned int rand = 0xACE1U; /* Any nonzero start state will work. */

                                      /*check for valid range.*/
                                      if(start_range == end_range) {
                                      return start_range;
                                      }

                                      /*get the random in end-range.*/
                                      rand += 0x3AD;
                                      rand %= end_range;

                                      /*get the random in start-range.*/
                                      while(rand < start_range){
                                      rand = rand + end_range - start_range;
                                      }

                                      return rand;
                                      }

                                      int main(void)
                                      {
                                      int i;
                                      for (i = 0; i < 0xFF; i++)
                                      {
                                      printf("%ut",MyRand(10,20));
                                      }
                                      return 0;
                                      }





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        The smallest and simple random generator which work with ranges is provided below with fully working example.



                                        unsigned int MyRand(unsigned int start_range,unsigned int end_range)
                                        {
                                        static unsigned int rand = 0xACE1U; /* Any nonzero start state will work. */

                                        /*check for valid range.*/
                                        if(start_range == end_range) {
                                        return start_range;
                                        }

                                        /*get the random in end-range.*/
                                        rand += 0x3AD;
                                        rand %= end_range;

                                        /*get the random in start-range.*/
                                        while(rand < start_range){
                                        rand = rand + end_range - start_range;
                                        }

                                        return rand;
                                        }

                                        int main(void)
                                        {
                                        int i;
                                        for (i = 0; i < 0xFF; i++)
                                        {
                                        printf("%ut",MyRand(10,20));
                                        }
                                        return 0;
                                        }





                                        share|improve this answer













                                        The smallest and simple random generator which work with ranges is provided below with fully working example.



                                        unsigned int MyRand(unsigned int start_range,unsigned int end_range)
                                        {
                                        static unsigned int rand = 0xACE1U; /* Any nonzero start state will work. */

                                        /*check for valid range.*/
                                        if(start_range == end_range) {
                                        return start_range;
                                        }

                                        /*get the random in end-range.*/
                                        rand += 0x3AD;
                                        rand %= end_range;

                                        /*get the random in start-range.*/
                                        while(rand < start_range){
                                        rand = rand + end_range - start_range;
                                        }

                                        return rand;
                                        }

                                        int main(void)
                                        {
                                        int i;
                                        for (i = 0; i < 0xFF; i++)
                                        {
                                        printf("%ut",MyRand(10,20));
                                        }
                                        return 0;
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 20 '18 at 20:54









                                        HaSeeB MiRHaSeeB MiR

                                        167310




                                        167310























                                            0














                                            If you're not generating your numbers too fast (*1) and your upper limit is low enough (*2) and your "time of day" includes nanoseconds, just use those nanoseconds.



                                            #include <stdio.h>
                                            #include <stdlib.h>
                                            #include <time.h>

                                            int nanorand(void) {
                                            struct timespec p[1];
                                            clock_gettime(CLOCK_MONOTONIC, p);
                                            return p->tv_nsec % 1000;
                                            }

                                            int main(void) {
                                            int r, x;
                                            for (;;) {
                                            r = nanorand();
                                            do {
                                            printf("please type %d (< 50 quits): ", r);
                                            fflush(stdout);
                                            if (scanf("%d", &x) != 1) exit(EXIT_FAILURE);
                                            } while (x != r);
                                            if (r < 50) break;
                                            }
                                            puts("");
                                            return 0;
                                            }


                                            And a sample run ...




                                            please type 769 (< 50 quits): 769
                                            please type 185 (< 50 quits): 185
                                            please type 44 (< 50 quits): 44


                                            (*1) if you're using them interactively, one at a time

                                            (*2) if you want numbers up to about 1000






                                            share|improve this answer






























                                              0














                                              If you're not generating your numbers too fast (*1) and your upper limit is low enough (*2) and your "time of day" includes nanoseconds, just use those nanoseconds.



                                              #include <stdio.h>
                                              #include <stdlib.h>
                                              #include <time.h>

                                              int nanorand(void) {
                                              struct timespec p[1];
                                              clock_gettime(CLOCK_MONOTONIC, p);
                                              return p->tv_nsec % 1000;
                                              }

                                              int main(void) {
                                              int r, x;
                                              for (;;) {
                                              r = nanorand();
                                              do {
                                              printf("please type %d (< 50 quits): ", r);
                                              fflush(stdout);
                                              if (scanf("%d", &x) != 1) exit(EXIT_FAILURE);
                                              } while (x != r);
                                              if (r < 50) break;
                                              }
                                              puts("");
                                              return 0;
                                              }


                                              And a sample run ...




                                              please type 769 (< 50 quits): 769
                                              please type 185 (< 50 quits): 185
                                              please type 44 (< 50 quits): 44


                                              (*1) if you're using them interactively, one at a time

                                              (*2) if you want numbers up to about 1000






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                If you're not generating your numbers too fast (*1) and your upper limit is low enough (*2) and your "time of day" includes nanoseconds, just use those nanoseconds.



                                                #include <stdio.h>
                                                #include <stdlib.h>
                                                #include <time.h>

                                                int nanorand(void) {
                                                struct timespec p[1];
                                                clock_gettime(CLOCK_MONOTONIC, p);
                                                return p->tv_nsec % 1000;
                                                }

                                                int main(void) {
                                                int r, x;
                                                for (;;) {
                                                r = nanorand();
                                                do {
                                                printf("please type %d (< 50 quits): ", r);
                                                fflush(stdout);
                                                if (scanf("%d", &x) != 1) exit(EXIT_FAILURE);
                                                } while (x != r);
                                                if (r < 50) break;
                                                }
                                                puts("");
                                                return 0;
                                                }


                                                And a sample run ...




                                                please type 769 (< 50 quits): 769
                                                please type 185 (< 50 quits): 185
                                                please type 44 (< 50 quits): 44


                                                (*1) if you're using them interactively, one at a time

                                                (*2) if you want numbers up to about 1000






                                                share|improve this answer















                                                If you're not generating your numbers too fast (*1) and your upper limit is low enough (*2) and your "time of day" includes nanoseconds, just use those nanoseconds.



                                                #include <stdio.h>
                                                #include <stdlib.h>
                                                #include <time.h>

                                                int nanorand(void) {
                                                struct timespec p[1];
                                                clock_gettime(CLOCK_MONOTONIC, p);
                                                return p->tv_nsec % 1000;
                                                }

                                                int main(void) {
                                                int r, x;
                                                for (;;) {
                                                r = nanorand();
                                                do {
                                                printf("please type %d (< 50 quits): ", r);
                                                fflush(stdout);
                                                if (scanf("%d", &x) != 1) exit(EXIT_FAILURE);
                                                } while (x != r);
                                                if (r < 50) break;
                                                }
                                                puts("");
                                                return 0;
                                                }


                                                And a sample run ...




                                                please type 769 (< 50 quits): 769
                                                please type 185 (< 50 quits): 185
                                                please type 44 (< 50 quits): 44


                                                (*1) if you're using them interactively, one at a time

                                                (*2) if you want numbers up to about 1000







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 27 '18 at 11:59

























                                                answered Nov 27 '18 at 11:52









                                                pmgpmg

                                                84.1k997167




                                                84.1k997167























                                                    -2














                                                    #include<stdio.h>
                                                    #include<conio.h>
                                                    #include<stdlib.h>
                                                    int main()
                                                    {
                                                    unsigned int x,r,i;
                                                    // no of random no you want to generate
                                                    scanf("%d",&x);
                                                    // put the range of random no
                                                    scanf("%d",&r);
                                                    unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
                                                    for(i=0;i<x;i++)
                                                    printf("%d ",(a[i]%r)+1);
                                                    free(a);
                                                    getch();
                                                    return 0;
                                                    }





                                                    share|improve this answer
























                                                    • Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                      – eliasah
                                                      Aug 16 '15 at 8:05











                                                    • please stop using deprecated header files.

                                                      – HaSeeB MiR
                                                      Sep 22 '18 at 9:15
















                                                    -2














                                                    #include<stdio.h>
                                                    #include<conio.h>
                                                    #include<stdlib.h>
                                                    int main()
                                                    {
                                                    unsigned int x,r,i;
                                                    // no of random no you want to generate
                                                    scanf("%d",&x);
                                                    // put the range of random no
                                                    scanf("%d",&r);
                                                    unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
                                                    for(i=0;i<x;i++)
                                                    printf("%d ",(a[i]%r)+1);
                                                    free(a);
                                                    getch();
                                                    return 0;
                                                    }





                                                    share|improve this answer
























                                                    • Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                      – eliasah
                                                      Aug 16 '15 at 8:05











                                                    • please stop using deprecated header files.

                                                      – HaSeeB MiR
                                                      Sep 22 '18 at 9:15














                                                    -2












                                                    -2








                                                    -2







                                                    #include<stdio.h>
                                                    #include<conio.h>
                                                    #include<stdlib.h>
                                                    int main()
                                                    {
                                                    unsigned int x,r,i;
                                                    // no of random no you want to generate
                                                    scanf("%d",&x);
                                                    // put the range of random no
                                                    scanf("%d",&r);
                                                    unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
                                                    for(i=0;i<x;i++)
                                                    printf("%d ",(a[i]%r)+1);
                                                    free(a);
                                                    getch();
                                                    return 0;
                                                    }





                                                    share|improve this answer













                                                    #include<stdio.h>
                                                    #include<conio.h>
                                                    #include<stdlib.h>
                                                    int main()
                                                    {
                                                    unsigned int x,r,i;
                                                    // no of random no you want to generate
                                                    scanf("%d",&x);
                                                    // put the range of random no
                                                    scanf("%d",&r);
                                                    unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
                                                    for(i=0;i<x;i++)
                                                    printf("%d ",(a[i]%r)+1);
                                                    free(a);
                                                    getch();
                                                    return 0;
                                                    }






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Aug 16 '15 at 6:30









                                                    AwiAwi

                                                    1




                                                    1













                                                    • Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                      – eliasah
                                                      Aug 16 '15 at 8:05











                                                    • please stop using deprecated header files.

                                                      – HaSeeB MiR
                                                      Sep 22 '18 at 9:15



















                                                    • Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                      – eliasah
                                                      Aug 16 '15 at 8:05











                                                    • please stop using deprecated header files.

                                                      – HaSeeB MiR
                                                      Sep 22 '18 at 9:15

















                                                    Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                    – eliasah
                                                    Aug 16 '15 at 8:05





                                                    Can you bring an explanation for you code please! And Please read about how to write a good answer

                                                    – eliasah
                                                    Aug 16 '15 at 8:05













                                                    please stop using deprecated header files.

                                                    – HaSeeB MiR
                                                    Sep 22 '18 at 9:15





                                                    please stop using deprecated header files.

                                                    – HaSeeB MiR
                                                    Sep 22 '18 at 9:15











                                                    -2














                                                    One of the simplest random number generator which not return allways the same value:



                                                    uint16_t simpleRand(void)
                                                    {
                                                    static uint16_t r = 5531; //dont realy care about start value
                                                    r+=941; //this value must be relative prime to 2^16, so we use all values
                                                    return r;
                                                    }


                                                    You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.






                                                    share|improve this answer
























                                                    • Can someone explain the down vote?

                                                      – 12431234123412341234123
                                                      Dec 11 '17 at 11:39
















                                                    -2














                                                    One of the simplest random number generator which not return allways the same value:



                                                    uint16_t simpleRand(void)
                                                    {
                                                    static uint16_t r = 5531; //dont realy care about start value
                                                    r+=941; //this value must be relative prime to 2^16, so we use all values
                                                    return r;
                                                    }


                                                    You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.






                                                    share|improve this answer
























                                                    • Can someone explain the down vote?

                                                      – 12431234123412341234123
                                                      Dec 11 '17 at 11:39














                                                    -2












                                                    -2








                                                    -2







                                                    One of the simplest random number generator which not return allways the same value:



                                                    uint16_t simpleRand(void)
                                                    {
                                                    static uint16_t r = 5531; //dont realy care about start value
                                                    r+=941; //this value must be relative prime to 2^16, so we use all values
                                                    return r;
                                                    }


                                                    You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.






                                                    share|improve this answer













                                                    One of the simplest random number generator which not return allways the same value:



                                                    uint16_t simpleRand(void)
                                                    {
                                                    static uint16_t r = 5531; //dont realy care about start value
                                                    r+=941; //this value must be relative prime to 2^16, so we use all values
                                                    return r;
                                                    }


                                                    You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Aug 26 '16 at 10:52









                                                    1243123412341234123412312431234123412341234123

                                                    662511




                                                    662511













                                                    • Can someone explain the down vote?

                                                      – 12431234123412341234123
                                                      Dec 11 '17 at 11:39



















                                                    • Can someone explain the down vote?

                                                      – 12431234123412341234123
                                                      Dec 11 '17 at 11:39

















                                                    Can someone explain the down vote?

                                                    – 12431234123412341234123
                                                    Dec 11 '17 at 11:39





                                                    Can someone explain the down vote?

                                                    – 12431234123412341234123
                                                    Dec 11 '17 at 11:39


















                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7602919%2fhow-do-i-generate-random-numbers-without-rand-function%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Contact image not getting when fetch all contact list from iPhone by CNContact

                                                    count number of partitions of a set with n elements into k subsets

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks