Menu

Dev C++ random number generator

2008-02-17
2012-09-26
  • Mathew Thomas

    Mathew Thomas - 2008-02-17

    Hey guys,

    I am darkshade and I am new to programming in C++.

    At the moment I have to create a project which will play the traditional tic-tac-toe game.

    I got the proram to do the design for me. now its just that I am planning ot generate a random number between 0 and 8 so that when the user click a button, it will generate a number and will put a cross on that panel

    BTW, I have 9 panels on which the cross and noughts appear.

    But my random number generator generates the same random numbers and I get the cross on top of an already obtained panel.

    Please help me out.

    D

     
    • Mathew Thomas

      Mathew Thomas - 2008-02-21

      OK but still I need to generate 9 random numbers which are unique (i.e) they dont repeat again. But your link helps a lot. Thanks man.

      Ill let u guys know how Im doing.

      Thanks for all your advice and help.

       
      • cpns

        cpns - 2008-02-21

        I guess you did not get my point; they are not random numbers! They are a clearly defined set of numbers (0..8) in a random order.

        Start with an array:

        int chk_array[] = {0,1,2,3,4,5,6,7,8} ;

        Then apply the Knuth shuffle algorithm on that array.

        If you want to see how bad a shuffle can be if you do not apply sound statistical theory, take a look at this: http://www.codinghorror.com/blog/archives/001015.html

        Clifford

         
    • Anonymous

      Anonymous - 2008-02-17

      Try call srand() before calling rand()

      That start a new sequence of pseudo-random integers to be returned by rand().

      See the manual for details.

      HTH

       
    • Kurgusov

      Kurgusov - 2008-02-19

      heres the RNG I use as a function.

      include <ctime>

      include <cstdlib>

      int roll (int high, int low)
      {
      int range = high - low +1 ;
      int val = (rand() % range) + low ;
      return val ;
      }

      now you have your rn generator you get a random seed from the computer clock,
      e.g.

      int main (int argv, int* argc [])
      {
      int seed = time (NULL) ;
      srand (seed) ;

      //those two lines sets up the value of seed.
      //you only need to do this once for the duration of your program btw

      //then test your RNG

      for (int i=0; i<100; i++){
      std::cout << roll (100, 1) << " : " ;
      }

      system ("pause") ;
      return 0 ;

      }

      have fun!

       
    • Mathew Thomas

      Mathew Thomas - 2008-02-19

      hey I have a small idea of how to do it. I dont know If Im going the long way. but here it is. As I told u before, Im creating a Graphical Tic-tac toe game.

      Im testing whether it will work on a normal console application. if it works then I can implement it into my main program. here is the code which I have right now.

      include <stdio.h>

      include <conio.h>

      include <stdlib.h>

      include <time.h>

      int main (void)
      {
      int a, test, chk_array[9], chk_no, chk_dec = 9;
      srand(time(NULL));

      for(chk_no = 0; chk_no &lt; 9; ++chk_no)
      {
       chk_array[chk_no] = rand () % chk_dec;
      }
      
      for(chk_no = 0; chk_no &lt; 9; ++chk_no)
      {
        printf (&quot;chk_array [%d] [%d] \n&quot;, chk_no, chk_array[chk_no]);
      }
      

      system("PAUSE");
      return 0;

      }

      Now this code generates different random numbers every time it runs from the beginning.
      this is the output in console window.

      chk_array [0] [1]
      chk_array [1] [6] <--|
      chk_array [2] [3] | These two numbers are not supposed to be the same They should be
      chk_array [3] [2] | different.
      chk_array [4] [6] <--|
      chk_array [5] [8]
      chk_array [6] [0] <--|
      chk_array [7] [0] <--| Similarly these are also not supposed to be the same.
      chk_array [8] [1]
      ^
      ^
      ^
      This is the array number

      Now u know what my problem is. I need all different numbers. Please help me out.

      D

       
    • BiT

      BiT - 2008-02-19

      Something like this is what you are looking for

      include <iostream>

      include <time.h>

      define MAXNUMBER 9

      using namespace std;

      //Random Number Generator
      int RandomNumberGenerator(){
      return rand() % 8;
      }

      int main(int argc, char ** argv)
      {
      // Generate random seed
      srand(time(0));
      // initialize array
      int NumbersGenerated[MAXNUMBER][1];
      for(int i=0;i< MAXNUMBER; i++){
      // set array element to 0 or false
      NumbersGenerated[i][0] = 0;
      }
      for(int j=0; j<MAXNUMBER;j++){
      //holder for random generated number
      int temp;
      //used to exit while loop
      bool isUsed = false;
      while(isUsed==false){
      //set temp to generated number
      temp = RandomNumberGenerator();
      // check to see if it is false or has been used
      if(NumbersGenerated[temp][0] == 0){
      // set to true
      NumbersGenerated[temp][0] = 1;
      // tell our while loop to exit
      isUsed=true;
      // print it out
      cout << temp << endl;
      }
      }
      }

      system("pause");

      return(0);
      }

       
    • BiT

      BiT - 2008-02-19

      my bad...not enough coffee yet

      return rand() % 8;

      should be

      return rand() % 9;

       
    • cpns

      cpns - 2008-02-20

      You misunderstand the meaning of "random".

      If every number in the sequence were unique, it would not be random, once you'd drawn a 6, 6 would have a liklihood of zero, and the liklihood of any other number not yet drawn increases equally. That's not random by any definition!

      You are looking for a random order of non random values, not random values. You need a shuffle algorithm. http://en.wikipedia.org/wiki/Knuth_shuffle

      rand() produces a random number not a random order, moreover the chances are that in that short loop it is producing a unique number each time but the %9 operation is restricting the value to a small range. (12 % 9) has the same value as (21 % 9) for example, each is equally likley on each iteration. Even then if rand() produces n it is equally likley to produce another n as it is any other value - that is what random means - all values have an equal liklihood, without any influence from historical values.

      Clifford

       
    • cpns

      cpns - 2008-02-20

      >> I am darkshade...

      Hello Mathew Thomas ;-)

      That is why I created a new account. Sourceforge has a distict lack of respect for personal privacy. It was not always thus, they changed it after I joined up and without asking... <very rude word here>.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.