如何生成没有rand()函数的随机数?

时间:2021-11-03 22:52:00

I want to generate (pseudo) random numbers between 0 and some integer. I don't mind if they aren't too random. I have access to the current time of the day but not the rand function. Can anyone think of a sufficiently robust way to generate these? Perhaps, discarding some bits from time of day and taking modulo my integer or something?

我想生成(伪)随机数在0和某个整数之间。我不介意它们是不是太随意。我可以访问当前的时间,但不能访问rand函数。有人能想出一个足够健壮的方法来产生这些东西吗?也许,从一天的时间中丢弃一些位并取我的整型或其他什么?

I am using c.

我使用c。

8 个解决方案

#1


20  

If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.

如果你想要一个超简单的伪随机生成器,你可以使用一个线性反馈移位寄存器。

The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)

*的文章中有一些代码片段供您参考,但基本上16位生成器的代码应该是这样的(从该页中轻轻修改…)

  unsigned short lfsr = 0xACE1u;
  unsigned bit;

  unsigned rand()
  {
    bit  = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
    return lfsr =  (lfsr >> 1) | (bit << 15);
  }

#2


7  

Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.

看看如何实现一个伪随机生成器(“内部”rand()),例如Mersenne twister就被高度重视。

#3


7  

For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.

对于“不太随机”的整数,可以从当前UNIX时间开始,然后使用递归公式r = ((r * 7621) + 1) % 32768;0(含)和M(排他性)之间的第n个随机整数在第n次迭代后是r % M。

This is called a linear congruential generator.

这叫做线性同余发电机。

The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...

递归公式是bzip2在其快速排序实现中用于选择pivot的方法。我不知道还有什么其他的用途,但是对于这个特殊的用途来说,它非常有用。

#4


0  

The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator

惟一“健壮”(不容易预测)的方法是编写自己的伪随机数生成器,并使用当前时间对其进行播种。*的链接:http://en.wikipedia.org/wiki/Pseudorandom_number_generator

#5


0  

You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html

你可以在这里找到“微型Mersenne Twister”:http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html。

it is pure c and simple to use. E.g. just using time:

它是纯c的,使用起来很简单。例如,只使用时间:

#include "tinymt32.h"
// And if you can't link:
#include "tinymt32.c"

#include <time.h>
#include <stdio.h>

int main(int argc, const char* argv[])
{
    tinymt32_t state;
    uint32_t seed = time(0);

    tinymt32_init(&state, seed);

    for (int i=0; i<10; i++)
            printf("random number %d: %u\n", i, (unsigned int)tinymt32_generate_uint32(&state));
}

#6


-1  

Below program generated random number,without using rand fuction

下面的程序生成随机数,不使用兰德函数

#include<stdio.h>
#include<time.h>
int main()
{
    int num;
    time_t sec;
    sec=time(NULL);
    printf("Enter The Number\n");
    scanf("%d",&num);
    if(num>0)
    {
        for(;;)
        {
            sec=sec%3600;
            if(num>=sec)
            {
            printf("%ld\n",sec);
            break;
            }
            sec=sec%num;
        }
    }
    else
    {
        printf("Please Enter Positive Value\n\n\n*****Thanks For Visit*****\n\n\n");
    }
    return 0;

}

May be it is useful

它可能有用吗

#7


-2  

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
unsigned int x,r,i;
// no of random no you want to generate
scanf("%d",&x);
// put the range of random no 
scanf("%d",&r);
unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
for(i=0;i<x;i++)
printf("%d ",(a[i]%r)+1);   
free(a);
getch();
return 0;
}

#8


-2  

One of the simplest random number generator which not return allways the same value:

一个最简单的随机数生成器,它不返回相同的值:

uint16_t simpleRand(void)
  {
    static uint16_t r = 5531; //dont realy care about start value
    r+=941; //this value must be relative prime to 2^16, so we use all values
    return r;
  }  

You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.

如果不希望序列总是以相同的值开始,那么您可能有时间来设置起始值。

#1


20  

If you're after an ultra-simple pseudo-random generator, you can just use a Linear Feedback shift Register.

如果你想要一个超简单的伪随机生成器,你可以使用一个线性反馈移位寄存器。

The wikipedia article has some code snippets for you to look at, but basically the code for a 16-bit generator will look something like this (lightly massaged from that page...)

*的文章中有一些代码片段供您参考,但基本上16位生成器的代码应该是这样的(从该页中轻轻修改…)

  unsigned short lfsr = 0xACE1u;
  unsigned bit;

  unsigned rand()
  {
    bit  = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
    return lfsr =  (lfsr >> 1) | (bit << 15);
  }

#2


7  

Look at implementing a pseudo-random generator (what's "inside" rand()) of your own, for instance the Mersenne twister is highly-regarded.

看看如何实现一个伪随机生成器(“内部”rand()),例如Mersenne twister就被高度重视。

#3


7  

For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768;. The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration.

对于“不太随机”的整数,可以从当前UNIX时间开始,然后使用递归公式r = ((r * 7621) + 1) % 32768;0(含)和M(排他性)之间的第n个随机整数在第n次迭代后是r % M。

This is called a linear congruential generator.

这叫做线性同余发电机。

The recursion formula is what bzip2 uses to select the pivot in its quicksort implementation. I wouldn't know about other purposes, but it works pretty well for this particular one...

递归公式是bzip2在其快速排序实现中用于选择pivot的方法。我不知道还有什么其他的用途,但是对于这个特殊的用途来说,它非常有用。

#4


0  

The only "robust" (not easily predictable) way of doing this is writing your own pseudo-random number generator and seeding it with the current time. Obligatory wikipedia link: http://en.wikipedia.org/wiki/Pseudorandom_number_generator

惟一“健壮”(不容易预测)的方法是编写自己的伪随机数生成器,并使用当前时间对其进行播种。*的链接:http://en.wikipedia.org/wiki/Pseudorandom_number_generator

#5


0  

You can get the "Tiny Mersenne Twister" here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html

你可以在这里找到“微型Mersenne Twister”:http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html。

it is pure c and simple to use. E.g. just using time:

它是纯c的,使用起来很简单。例如,只使用时间:

#include "tinymt32.h"
// And if you can't link:
#include "tinymt32.c"

#include <time.h>
#include <stdio.h>

int main(int argc, const char* argv[])
{
    tinymt32_t state;
    uint32_t seed = time(0);

    tinymt32_init(&state, seed);

    for (int i=0; i<10; i++)
            printf("random number %d: %u\n", i, (unsigned int)tinymt32_generate_uint32(&state));
}

#6


-1  

Below program generated random number,without using rand fuction

下面的程序生成随机数,不使用兰德函数

#include<stdio.h>
#include<time.h>
int main()
{
    int num;
    time_t sec;
    sec=time(NULL);
    printf("Enter The Number\n");
    scanf("%d",&num);
    if(num>0)
    {
        for(;;)
        {
            sec=sec%3600;
            if(num>=sec)
            {
            printf("%ld\n",sec);
            break;
            }
            sec=sec%num;
        }
    }
    else
    {
        printf("Please Enter Positive Value\n\n\n*****Thanks For Visit*****\n\n\n");
    }
    return 0;

}

May be it is useful

它可能有用吗

#7


-2  

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
unsigned int x,r,i;
// no of random no you want to generate
scanf("%d",&x);
// put the range of random no 
scanf("%d",&r);
unsigned int *a=(unsigned int*)malloc(sizeof(unsigned int)*x);
for(i=0;i<x;i++)
printf("%d ",(a[i]%r)+1);   
free(a);
getch();
return 0;
}

#8


-2  

One of the simplest random number generator which not return allways the same value:

一个最简单的随机数生成器,它不返回相同的值:

uint16_t simpleRand(void)
  {
    static uint16_t r = 5531; //dont realy care about start value
    r+=941; //this value must be relative prime to 2^16, so we use all values
    return r;
  }  

You can maybe get the time to set the start value if you dont want that the sequence starts always with the same value.

如果不希望序列总是以相同的值开始,那么您可能有时间来设置起始值。