Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

random brojevi ~ kako najbrze i najlakshe ?

[es] :: C/C++ programiranje :: C/C++ za početnike :: random brojevi ~ kako najbrze i najlakshe ?

[ Pregleda: 2057 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

cHeMiR
Beograd

Član broj: 147203
Poruke: 55



+13 Profil

icon random brojevi ~ kako najbrze i najlakshe ?09.09.2007. u 17:57 - pre 202 meseci
kako da napravim program koji ce nasumice da bira brojeve do nekog broja koji je zadat ?
npr. da izabere jedan broj od 6, kao kada bacimo kockicu
ima neka funkcijavaljda neshto kao y=random (6)...

precheshljao sam forum i nisam nashao nishta mog nivoa znanja tako da...
ako sam preskochio neku temu gde bih nashao odgovor, molim linkujte ovde

hvala unapred
 
Odgovor na temu

ventura

Član broj: 32
Poruke: 7781
62.162.230.*



+6455 Profil

icon Re: random brojevi ~ kako najbrze i najlakshe ?09.09.2007. u 18:11 - pre 202 meseci
Code:

/***************************************************************************
 * rnd.C
 *
 * exports:  rnd(n)  -- returns a "random" integer in {0,1, ..., n-1}
 *           drnd(r) -- returns a "random" real in [0,r)
 *                 
 * P Rogaway - 10/95
 *
 * NOTE: uses brain-dead mechanisms;  These routines are inappropriate for 
 * applications requiring "unguessable" pseudorandom numbers (e.g., for 
 * cryptographic purposes).
 ***************************************************************************
 */

#ifndef IOSTREAMH
#include <iostream.h>
#endif
#ifndef __MATH_H__
#include <math.h>
#endif
#ifndef _SYS_TIME_H
#include <sys/time.h>
#endif
#ifndef __STDLIB_H__
#include <stdlib.h>
#endif 

#ifndef _SGI_SOURCE
  extern "C" int gettimeofday(struct timeval *tp,struct timezone *tz);
#endif

static const same_random_numbers_each_run = 0; // for debug, sometimes  useful
                                               // to set this to 1, so one 
                                               // always gets the same
                                               // "random" numbers.

/*
 * getseed 
 *    get a seed (just 32 bits) for the pseudorandom generator, using
 *    the simplest possible thing (time returned by gettimeofday()).
 */
static long getseed()
{
  if (same_random_numbers_each_run) {
    return 1;
  } 
  else {
    struct timeval tp; 
    struct timezone tzp;
    if (gettimeofday(&tp, &tzp)) { 
       cerr << "getseed:  gettimeofday failed\n";
       return 0;
    }
    return tp.tv_usec;
  }
}


///////////////////////////////////////////////////////////
// rnd(n) -- return a "random" integer in [0..n-1]
///////////////////////////////////////////////////////////
int rnd(int n)
{
  static first_time=1;                                       
  static char state[256];                                   
  if (first_time) {                                        
    first_time = 0;                                       
    unsigned int seed = (unsigned int) getseed();
    initstate(seed, state, 256);                           
  }
  if (n<0) {cerr << "Bad argument to rnd()\n"; return 0;}
  unsigned long mask = 1;
  while (mask<n) mask = 2*mask+1;
  for (;;) {
    long r = random();
    if ((r&mask) < n) return r&mask;
  }



///////////////////////////////////////////////////////////
// drnd(r) -- return a "random" double in [0,.r)
///////////////////////////////////////////////////////////
double drnd(double r)
{
  static first_time=1;                                       
  if (first_time) {                                        
    first_time = 0;                                       
    long seed = long(getseed()); 
    srand48(seed);
  }
  return r*drand48();
}
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.co.yu.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: random brojevi ~ kako najbrze i najlakshe ?09.09.2007. u 18:14 - pre 202 meseci
Header:
#include <stdlib.h>

Prototip:
int rand(void);

Primer:
Code:

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

int main()
{
   int i;
   randomize();
   printf( "Deset slucajnih brojeva izmedjuu 1 i 6\n\n" );
   for ( i=0; i<10; i++ )
      printf( "%d. slucajan broj: %d\n", i+1, rand()%6 + 1 );
   system( "PAUSE" );
   return 0;
}


Google za više informacija:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html
http://www.thinkage.ca/english/gcos/expl/c/lib/rand.html
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: random brojevi ~ kako najbrze i najlakshe ?

[ Pregleda: 2057 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.