Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Random numbers (C)

Options
  • 27-03-2003 8:12pm
    #1
    Closed Accounts Posts: 3,322 ✭✭✭


    I have a simple piece of code to generate 6 random numbers between 0 and 100, however every time I run the program it generates the same numbers:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
        int i = 0;
        int array[6];
        for (i=0; i<6; i++)
            array[i] = rand()%100;
        for (i=0; i<6; i++)
            printf ("%d ", array[i]);
    }
    

    I've tried fflush(stdout) at the beginning of the program but get the same problem - anyone know how to? I know if you use srand() with a different seed it gets past the problem but I'd rather not do that..
    Thx in advance


Comments

  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,514 Mod ✭✭✭✭BossArky


    I know you said that you didn't want to use srand...but can't you use it with the pc clock time...in c++ anyway...just a thought..


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by Repli
    I know if you use srand() with a different seed it gets past the problem but I'd rather not do that..

    That's a bit like saying you want to get rid of your first, but would rather not drink. What's wrong with srand?


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    Ok but I didn't know how to give srand a different number each time the program is called..
    Bossarky gave me a good idea there.. I could pass srand time(0) or something each time the program is called..


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Advertisement
  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    if you are going to be using random numbers just start your program:
    int main (int argc, char** argv)
    {
        srand(GetTickCount());
        ....
    





    Using the modulus N operator does *not* give a proper random number between 0 and N. My statistics lecturer spent an entire lecture explaining why. Boring as hell.

    For true randomness:
    int random_int (const int& high, const int& low = 0)
    {
        assert (high > low && "Random_int requires High > Low");
    
        double random = (double)rand() / (double)RAND_MAX;
        double answer = (random * (double)(high - low)) + (double)low;
        return (int)answer;
    }
    

    Overkill however :)


  • Closed Accounts Posts: 7,346 ✭✭✭Rev Hellfire


    thats not true random, nowhere near it.

    if u want a easy sorce of entropy, your best bet is to look at the intel RNG assuming you're using a pentium III+, the athlons also have one. Or dig deep into your pockets and get HSM.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Remember coming accross a set of trig equations which if you fed 3 numbers generated by srand() would return something a little more random. I've noticed myself when using srand that if you run the program serveral times one after the other it will generate the same number a few times as the clock seed is close to the last time it ran and you're rounding it off so much. I can't for the life of me remember where or what those trig equations are but if you have a look at the game development sites you should find something similar. That's where I found em in the first place.


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    heres some code to generate a ramdom number between 100 and 500 and store it in an array
    some tidying up needs to be done but itll give you an idea of how to generate a totally random number

    ps half the variables prob arent needed..!!!
    i just to lazy to go messing bout with it

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

    #define maxarraysize 100
    #define maxvalue 500
    #define minvalue 100

    void main(void)
    {
    int countletter = 0; // var used to traverse to the end of the array
    int random_number = 0; // var to store the random num in the array a temp var
    int numarray[maxarraysize]; // array for numbers
    int choosen_value = 0; // inputed value by the user
    int occurrences = 0; // num of occ of the num in the list
    int array_pos[maxarraysize]; // array to store location of the choosen value in the numarray
    int index = 0; // counter
    int valuepos = 0; // variable to store the value of the location of the choosen value
    int rownum = 0,colnum = 0; // row and col of the choosen num
    srand(time(NULL)); // used to set a seed value in the rand
    //func so that totaolly different everytime that you use it

    for(countletter;countletter < maxarraysize;countletter++)
    {
    random_number = rand() % maxvalue; // used to generate a random value
    if ((random_number >= minvalue) && (random_number <= maxvalue)) // check to make sure that the value between 100 and 500
    {
    numarray[countletter] = random_number;
    }
    else
    {
    countletter--;
    }
    array_pos[countletter] = 0;
    }


  • Advertisement
Advertisement