佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1070|回复: 15

RANDOM GENERATOR!!

[复制链接]
发表于 10-10-2008 05:24 PM | 显示全部楼层 |阅读模式
我用了个RANDOM NUMBER GENERATOR来GENERATE 六个号码 (全在一样的ARRAY), 那GENERATOR成功GENERATE出在USER要求的RANGE之中....但它会GENERATE会重复的号码....要如何避免它GENERATE两个一样的号码??

        for (index = 0; index < 6; index++)
        {
                pcvalues[index] = (rand() + time (0)) % top ;
                if (pcvalues[index] < low)
                {
                        do
                {
                        pcvalues[index] = (pcvalues[index] + low ) % top;
                } while (pcvalues[index] < low);
                }
                                
        }


以上是GENERATOR的SCRIPT, c++;

TOP = MAXIMUM VALUE (MAX RANGE)
LOW = MINIMUM VALUE (MIN RANGE)

谢谢解答

[ 本帖最后由 strikefred0m 于 10-10-2008 05:26 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 10-10-2008 08:24 PM | 显示全部楼层

回复 1# strikefred0m 的帖子

rand() 一直重复相同的好吗是因为你没更换rand seed, 用srand 来更换

#include <ctime>
.....

srand( time(0) );
< your code here >

...

return 0;
}
回复

使用道具 举报

 楼主| 发表于 10-10-2008 10:42 PM | 显示全部楼层

回复 2# onlylonly 的帖子

没有, 我用过了, SRAND在1-7 RANGE也会重复, RAND() * TIME (0) 不会一直一样, 但有机会GENERATE会一样的号码, 跟SRAND一样的...
回复

使用道具 举报

发表于 10-10-2008 10:44 PM | 显示全部楼层
原帖由 onlylonly 于 10-10-2008 08:24 PM 发表
rand() 一直重复相同的好吗是因为你没更换rand seed, 用srand 来更换

#include
.....

srand( time(0) );
< your code here >

...

return 0;
}


觉得rand()不是很好用咯...generate出来的random number不是很random...有没有更好的3rd party介绍?
回复

使用道具 举报

发表于 10-10-2008 11:19 PM | 显示全部楼层

回复 3# strikefred0m 的帖子

你是说, 你要第一个array 与下一个 array 里不可是相同?

比如, 第一次
4 2 3 1 2 4

下一次就不可generate 4 2 3 1 2 4?

可以用以个 swap 来暂时寄放上一个array 的 value, 然后再来 compare
  1. #include <iostream>
  2. #include <cctype>
  3. #include <ctime>

  4. using namespace std;

  5. void print_array( int array[]);
  6. void generate_array(int array[], int max, int min);
  7. void copy_array(int swap[], int array[]);
  8. bool compare_array(int swap[], int array[]);

  9. int main()
  10. {
  11.     srand( time(0) );

  12.     const int TOP = 20, LOW = 10;

  13.     int array[6], swap[6],
  14.         i;

  15.     generate_array( array, TOP, LOW );

  16.     print_array( array );

  17.     copy_array( swap, array );

  18.     while( compare_array( swap, array ) )
  19.         generate_array( array, TOP, LOW );

  20.     print_array( array );

  21.     return 0;

  22. }

  23. void print_array( int array[])
  24. {
  25.     int i;

  26.     for( i = 0; i < 6; i++)
  27.         cout << array[i] << ' ';

  28.     cout << endl;
  29. }


  30. void generate_array( int array[], int max, int min )
  31. {
  32.     int i;

  33.     for( i = 0; i < 6; i++)
  34.         array[i] = (rand() % min) + max;

  35. }

  36. void copy_array( int swap[], int array[])
  37. {
  38.     int i;

  39.     for( i = 0; i < 6; i++)
  40.         swap[i] = array[i];
  41. }

  42. bool compare_array(int swap[], int array[])
  43. {
  44.     int i;

  45.     for( i = 0; i < 6; i++)
  46.         if( swap[i] != array[i] )
  47.             return false;

  48.     return true;

  49. }
复制代码
加入while, for loop 来print 更多次的array

[ 本帖最后由 onlylonly 于 10-10-2008 11:21 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 11-10-2008 12:23 AM | 显示全部楼层

回复 5# onlylonly 的帖子

是里面的那六个号码不要一样.

比如, 第一次
4 2 3 1 2 4

我就是不要它出现两次4
回复

使用道具 举报

Follow Us
发表于 11-10-2008 01:39 AM | 显示全部楼层
不好意思楼主,帮不到你。可能还需要你帮忙
为什么我做的PROGRAM,RUN的时候号码一直会重复的?EXP, 032323, 014141
我知道这是允许的,(只要【Nth+1】的号码不要跟【Nth】的号码一样就可以)可是该不会那么巧吧?
会不会是我的CODE的问题
看了很久都搞不懂。
那位大大可以帮忙看吗?
我的CODE如下,
#include <iostream.h>
#include <time.h>
#include <stdlib.h>

int main()
{
        int x=0, y=0, k=0;
        for (int i=0; i<6; i++)
        {
                cout<<
                y=x;
                srand(time(NULL));
                k=rand()%6;

                while (k==y)
                {        k=rand()%6;        
                        x=k;}
                if (k!=y)
                        {x=k;}               
        }
        return 0;
}

[ 本帖最后由 lliioonn 于 11-10-2008 01:46 AM 编辑 ]
回复

使用道具 举报

发表于 11-10-2008 09:14 AM | 显示全部楼层

回复 6# strikefred0m 的帖子

这个容易, 只要每次generate 过后, compare 之前的号码,如果是一样, 那么重新 generate 就可以了
  1. #include <iostream>
  2. #include <cctype>
  3. #include <ctime>

  4. using namespace std;

  5. int main()
  6. {
  7.     srand( time(0) );

  8.     int i, j, array[6];
  9.     const int MAX = 20, MIN = 10;

  10.     for( i = 0; i < 6; i++ )
  11.     {
  12.         array[i] = (rand() % MIN) + MAX;

  13.         for( j = 0; j < i; j++)
  14.         {
  15.             //uncomment to see how the actual program works
  16.             //by using loop, check each array with its precedence, if they are the same, regenerate the number
  17.             //cout << "array[" << i << "] = "  << array[i] << endl
  18.             //     << "array[" << j << "] = "  << array[j] << endl << endl;

  19.             if( array[i] == array[j])
  20.             {
  21.                 array[i] = (rand() % MIN) + MAX;
  22.                 j = 0; //recheck
  23.                 continue;
  24.             }
  25.         }
  26.     }

  27.     for( i = 0; i < 6; i++)
  28.         cout << array[i] << ' ';

  29.     return 0;

  30. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 11-10-2008 03:25 PM | 显示全部楼层
谢谢, 可以了
回复

使用道具 举报

发表于 12-10-2008 12:58 PM | 显示全部楼层
to onlyonly, array = (rand() % MIN) + MAX; This will generate number greater than the maximum boundary, which is 20, in that case.

Here's a more efficient way.

  1. #include
  2. #include
  3. #include

  4. using namespace std;

  5. int main()
  6. {
  7.     srand(time(0));

  8.     const int MAX = 20, MIN = 10;
  9.     int i, array[6], gap = MAX - MIN;
  10.     bool binaryArray[gap];

  11.     // initialize the array elements to false
  12.     for( i = 0; i < gap; i++)
  13.         binaryArray[i] = false; // false means that the number mapped by this array is unique

  14.     for( i = 0; i < 6; i++ )
  15.     {
  16.         do
  17.         {
  18.             array[i] = (rand() % MIN) + (MAX-MIN); // generate random number >= min, <= max
  19.         }
  20.         while(binaryArray[array[i] - gap]); // loops until the generated number is unique

  21.         binaryArray[array[i] - gap] = true; // mark that the number mapped by this index is no longer unique
  22.     }

  23.     for( i = 0; i < 6; i++)
  24.         cout << array[i] << ' ';

  25.     return 0;

  26. }
复制代码



[ 本帖最后由 solidx 于 12-10-2008 01:02 PM 编辑 ]
回复

使用道具 举报

发表于 12-10-2008 07:49 PM | 显示全部楼层

回复 10# solidx 的帖子

对对, 应该是 (rand() % min ) + ( max - min )

感谢这位仁兄提醒
回复

使用道具 举报

发表于 12-10-2008 07:52 PM | 显示全部楼层

回复 10# solidx 的帖子

其实我不用一个 flag 来确定是否是unique是为了节省 memory, 虽然只是多出 6 bytes

不过仁兄的code却让我看到了另一个思想模式,感谢感谢
回复

使用道具 举报

发表于 12-10-2008 09:10 PM | 显示全部楼层
嗯~ 我的重点是减少 unnecessary processing. 我只是提出另一个方案, 两个方法都正确, 看情况决定用哪一个罢了.
回复

使用道具 举报

发表于 12-10-2008 10:04 PM | 显示全部楼层

回复 13# solidx 的帖子

不过老实说, 看了这个code多我的确是收益良多。 我重来没想过可以以如此的方式来查看一个号码是否是unique的。。 感谢了
回复

使用道具 举报

发表于 15-10-2008 03:03 AM | 显示全部楼层
为什么我试RUNSOLIDX大大的CODE,有3个ERROR的,
那位大大能帮忙吗?
我GOOGLE了,可是看了不明白
int SIZE = 300 ;
bool a[SIZE] = { true };

This should work as long as ur C++ compiler defines a bool  data type ; btw, the while loop solution works as well ....

要怎样C++ compiler defines a bool  data type??
先谢了
回复

使用道具 举报

发表于 16-10-2008 01:48 PM | 显示全部楼层
Have you put this?
  1. #include
  2. #include

  3. using namespace std;
复制代码


If it's still not working, try change the compiler in the compiler option.

bool is already defined in C++ as primitive data type.

[ 本帖最后由 solidx 于 16-10-2008 01:50 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 01:30 PM , Processed in 3.827511 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表