🔒 Locked
Random Numbers
Advertisement
Original Post
to always have random numbers you need to set random with time
Matt
#include <time.h>
int number;
int main()
{
// do this a the begining of your program
srand(time(NULL));
for(int i=0; i<10; i++)
{
// this will give you a random number between 10 and 100
number = (rand() % 100) + 10;
printf("%d\n", number);
}
return 0;
}
Matt
quote:
Original post by 31337
What is the C standard way of generating a random number between x and y? Could somebody give me an example function? I've tried using rand() and srand() but I got predictable results![]()
Here is better psuedo random number generator, it called Mersenne Twister
As for generating numbers that fall in certain range, you can use the modulous operator(%). For example if you want number between 5 and 20.
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
int a = 0;
srand(time(NULL));
a = (rand() % 16) + 5;
return 0;
}
Sorry the example is C++ but you should still beable to get the jist.
Anyway hope this helps
[edited by - prh99 on October 16, 2003 10:48:34 PM]
Yes I''ve heard all of this random number theory, mainly that there is no such thing as a random number but that is only according to the philosophical approach. Mathematically I would say that you can claim numbers generated by observing sound card static impacted by the time, keystrokes, etc... could be considered random.
Thanks for the information guys. Here''s the problem though, I need to generate a lot of numbers all at the same time. What I was doing was:
But what I think was happening was that it was generating them in less than a millisecond or something because I got very bizare results. I''m going to try the time approach.
Thanks for the information guys. Here''s the problem though, I need to generate a lot of numbers all at the same time. What I was doing was:
for(int i=0; i<1000; i++) {
srand(GetTickCount());
int rand = rand() % 100;
}
But what I think was happening was that it was generating them in less than a millisecond or something because I got very bizare results. I''m going to try the time approach.
Hey elite (
), sometimes, you don''t want totaly random outputs, to allow you to reproduce bugs, keep networking in sync, and replay game events. That''s what car racing (mostly) on consoles do. They store the seed value, and the initial state of the game, the controler inputs, and from there, they can reproduce exactly the whole state of the race at any given time (although you are only limited to going forward in time). Instead of recording the positions of the cars and other objects throughout the game, It saves a lot of storage space. All the calls to the random number generator will produce the same results again and again when you hit replay. It''s tricky to get it right, but when you do, you feel like a more hip and a more socialy acceptable version of David Blaine. A bit of magic.
quote:
Original post by 31337
for(int i=0; i<1000; i++) {
srand(GetTickCount());
int rand = rand() % 100;
}
But what I think was happening was that it was generating them in less than a millisecond or something because I got very bizare results. I''m going to try the time approach.
This is a very common mistake. srand() *initializes* the pseudo-random number generator, so call it only once. Like this:
srand(GetTickCount());
for(int i=0; i<1000; i++) {
int rand = rand() % 100;
}
I read about a way to generating very high-quality random sequences using a computer and its microphone. In this article, the writer said that since background termic noise is indeed random, then recording it will result in random numbers.
Are they only good pseudorandom, or are they true random numbers?
Are they only good pseudorandom, or are they true random numbers?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement