开发者

Repeated random number in a loop

开发者 https://www.devze.com 2023-03-12 15:14 出处:网络
A need to generatera random numbers between 0 and 1 for every different loop. for ( 1 to 10000) a call RandomKey function so as to generate different random numbers.

A need to generatera random numbers between 0 and 1 for every different loop.

for ( 1 to 10000) a call RandomKey function so as to generate different random numbers. But the problem is every iteration i get same numbers.

RandomKey function is as follows:

 void RandomKey ()
 {
   srand((unsigned)time(0));
   for (int k=0;k<ActivityNumber;k++)
 {

Act_num[k].Priority=(rand()%10000)*0.0001;//random number

 }


for (int i=0;i<ActivityNumber;i++)
   arr[i]=Act_num[i].Priority;
开发者_如何学编程

How can i solve the problem?


Move the seeding function call (srand) outside of the function call (ie. at the beginning of your program, once).

Edit: on further thought, if you mean that you get all 0's in arr (if it's say... int), that's because rand()%10000)*0.0001 returns a number between 0 and 0.0003 or so, so if you cast them to int, you'll get 0.

Edit2: Nevermind, seems I guessed right the first time :)


Since the processor is fast, time(0) is the same. Call srand only once.


(I'm restating the other suggestions here.)

There are at least three things you should verify:

  • Only call srand once in your program
  • If srand is called more than once, or your program is run many times per second, then do not use time(0) to initialize srand.
  • If each random number should only be 0 or 1, you should call (int)(rand() & 1) to get uniformly distributed numbers from zero to one. If you need floats between 0.0 and 1.0, you need to do it differently.

EDIT: If you just want a random floating point number between zero and one, see Generating random number between [-1, 1] in C?


You should call srand() once, and only once, during the entire time your program runs.

Actually, you shouldn't call it at all because srand()/rand() are utterly broken on many machines. C++0x finally fixes this brokenness. If you can use either Boost or TR1 you can use the new and improved C++ random number right now rather than having to wait until C++0x finally becomes a standard and then wait longer for a vendor to implement it.

Other alternatives (although none are part of the standard) are srandom()/random() and srand48()/drand48(). The latter, if available, form a nice simple interface. drand48() produces a random double between 0 and 1. Or roll your own. Anything is better than rand().


Works for me:

But when you post code you should post code that is compilable (otherwise we are guessing). Also by making the code small enough to post here you will usually find the silly mistakes you make:

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

#include <algorithm>
#include <iterator>
#include <iostream>

int const iterationnumber = 5;
int const ActivityNumber  = 5;

double arr[5];                          // HERE is probably your mistake (double not int)
struct { double Priority; } Act_num[5]; // OR HERE make sure Priority is a double

void RandomKey();
int main ()
{
    srand((unsigned)time(0));

    for (int k=0;k<iterationnumber;k++)
    {
        RandomKey ();//i call randomkey for every iteration

        std::copy(&arr[0], &arr[ActivityNumber], std::ostream_iterator<double>(std::cout, " "));
        std::cout << "\n";
    }
}

void RandomKey ()
{
    for (int k=0;k<ActivityNumber;k++)
    {
        Act_num[k].Priority=(rand()%10000)*0.0001;//random number
    }

    for (int i=0;i<ActivityNumber;i++)
        arr[i] = Act_num[i].Priority;
}


This code generatesa unique random number only once.

#include <ctime>
# include <iostream>
using namespace std;


int main()
{

      int size=100;
      int random_once[100];
      srand(time(0));

      for (int i=0;i<size;i++)  // generate unique random number only once
      {
          random_once[i]=rand() % size;
          for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;   
      }

      for ( i=0;i<size;i++) cout<<" "<<random_once[i]<<"\t";

  return 0;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号