|
查看: 1071|回复: 15
|
RANDOM GENERATOR!!
[复制链接]
|
|
|
我用了个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 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 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- #include <iostream>
- #include <cctype>
- #include <ctime>
- using namespace std;
- void print_array( int array[]);
- void generate_array(int array[], int max, int min);
- void copy_array(int swap[], int array[]);
- bool compare_array(int swap[], int array[]);
- int main()
- {
- srand( time(0) );
- const int TOP = 20, LOW = 10;
- int array[6], swap[6],
- i;
- generate_array( array, TOP, LOW );
- print_array( array );
- copy_array( swap, array );
- while( compare_array( swap, array ) )
- generate_array( array, TOP, LOW );
- print_array( array );
- return 0;
- }
- void print_array( int array[])
- {
- int i;
- for( i = 0; i < 6; i++)
- cout << array[i] << ' ';
- cout << endl;
- }
- void generate_array( int array[], int max, int min )
- {
- int i;
- for( i = 0; i < 6; i++)
- array[i] = (rand() % min) + max;
- }
- void copy_array( int swap[], int array[])
- {
- int i;
- for( i = 0; i < 6; i++)
- swap[i] = array[i];
- }
- bool compare_array(int swap[], int array[])
- {
- int i;
- for( i = 0; i < 6; i++)
- if( swap[i] != array[i] )
- return false;
- return true;
- }
复制代码 加入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  |
|
|
|
|
|
|
|
|
|
|
发表于 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 就可以了- #include <iostream>
- #include <cctype>
- #include <ctime>
- using namespace std;
- int main()
- {
- srand( time(0) );
- int i, j, array[6];
- const int MAX = 20, MIN = 10;
- for( i = 0; i < 6; i++ )
- {
- array[i] = (rand() % MIN) + MAX;
- for( j = 0; j < i; j++)
- {
- //uncomment to see how the actual program works
- //by using loop, check each array with its precedence, if they are the same, regenerate the number
- //cout << "array[" << i << "] = " << array[i] << endl
- // << "array[" << j << "] = " << array[j] << endl << endl;
- if( array[i] == array[j])
- {
- array[i] = (rand() % MIN) + MAX;
- j = 0; //recheck
- continue;
- }
- }
- }
- for( i = 0; i < 6; i++)
- cout << array[i] << ' ';
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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.
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- srand(time(0));
- const int MAX = 20, MIN = 10;
- int i, array[6], gap = MAX - MIN;
- bool binaryArray[gap];
- // initialize the array elements to false
- for( i = 0; i < gap; i++)
- binaryArray[i] = false; // false means that the number mapped by this array is unique
- for( i = 0; i < 6; i++ )
- {
- do
- {
- array[i] = (rand() % MIN) + (MAX-MIN); // generate random number >= min, <= max
- }
- while(binaryArray[array[i] - gap]); // loops until the generated number is unique
- binaryArray[array[i] - gap] = true; // mark that the number mapped by this index is no longer unique
- }
- for( i = 0; i < 6; i++)
- cout << array[i] << ' ';
- return 0;
- }
复制代码
[ 本帖最后由 solidx 于 12-10-2008 01:02 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 12-10-2008 07:49 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 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?- #include
- #include
- 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 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|