Looking for a better randomization function.



  • isabeau, I didn't succeed on integrating the code given there with my project and I don't speak that language... :-\

    Can you or anyone alse integrate it and post the complete code?

    Thanks.



  • #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define FIRST_BET 2
    
    class Random{
    private:
        unsigned __int64 x;
    public:
        Random(unsigned __int64 seed){
            x=seed|(unsigned __int64(1)<<32);
        }
        unsigned int operator()(){
            x=1967773755*(x&0xffffffff)+(x>>32);
            return x;
        }
    }; 
    
    void main()
    {
        Random r(time(0));
    
        int bank=0, bet=FIRST_BET, wins=0, max_lost=0, num_max_lost=0, losts=0;
        bool bol=false;
    
        puts("What is your initial bank?");
        scanf("%d", &bank);
    
        while (bank>0 && bank<12345678912)
        {
    
            if (r()%2)
            {
                //WON
                bol = true;
    
                wins++;
                bank += bet;
                bet = FIRST_BET;
                losts=0;
    
            }
            else
            {
                //LOST
                bol = false;
    
                losts++;
                bank-= bet;
    
                if (losts>max_lost)
                {
                    max_lost = losts;
                    num_max_lost=1;            }
                else if (losts==max_lost)
                {
                    num_max_lost++;
                }
    
                bet *= 2;
            }
        }
    
        printf("\nRESULTS\n");
    
        printf("\nbank = %d\nwins = %d\nmax_lost = %d\nnum_max_lost = %d\nlosts = %d\n\n",
                bank, wins, max_lost, num_max_lost, losts);
    
        if (bol)
            printf("WIN");
        else
            printf("LOST");
    
    }
    


  • Thanks a lot people !
    Now that program actually shows that the "doubling" system will leave you broke 🙂
    This function gives much better results in my humble opinion...

    Just two more small questions.

    1. How can I ask for numbers between 0-5 for example?
    2. How can I print an unsigned-long-type-variable ?
      I've tried using printf("ul", ulvar); and printf("l", ulvar); and printf("l", ulvar); but none printed the var 123456789123 as it is.

    Lots and lots of thanks 2 all of you!!! 👍 👍 👍



  • ice-man6969 schrieb:

    1. How can I ask for numbers between 0-5 for example?

    just use "r()%6" as your calculation number (in general, to generate numbers between minv and maxv, you need "rand()%(maxv-minv+1)+minv")

    1. How can I print an unsigned-long-type-variable ?
    printf("%lu",val);
    

    (%u displays an int as unsigned value, the l modifier denotes to use long instead of int)



  • I g2g now so i'll try to use the functions later and let you know how I did 🙂

    Thanks a lot !



  • Something is messed up...
    I've tried using this code:

    unsigned long l;
    
    	puts("enter a number:");
    	scanf("%lu", &l);
    
    	printf("\n\n %lu \n\n", l);
    

    But is prints some other number instead of 12345678912 and all the numbers above this one...

    How come?



  • Is your unsigned long a 64 bit type? Otherwise, 12345678912 won't fit.



  • I don't know but I succeeded doing scan and print with an int with the number 1234567891 so I thought that unsigned long could contain more digits...

    I don't seem to make it work. I've tried all kind of things but nothing was successful. :-\



  • ice-man6969 schrieb:

    so I thought that unsigned long could contain more digits...

    The standard says that long is at least as big as int, but it doesn't have to be larger.

    What do you get for sizeof(unsigned long)?

    The maximum value you can store in an unsigned 32 bit value is 2^32 - 1 = 4,294,967,295.



  • sizeof(unsigned long) = 4
    but also
    sizeof(unsigned int) = 4

    so what is the use of long ?????? :-\



  • ice-man6969 schrieb:

    so what is the use of long ?????? :-\

    The standard requires this type. That's all there is to it.

    You can use unsigned __int64. The format specifier for printf and scanf is %I64u. Note that this is Microsoft-specific.



  • Thanks MFK !!!

    And again thanks to everyone !!!

    👍 👍 👍 👍 👍 👍 👍 👍 👍 👍


Anmelden zum Antworten