Solved

Random number generator in (Linux with g++)

Posted on 2000-02-18
6
667 Views
Last Modified: 2006-11-17
I have just discovered an anamoly in a pece of code I wrote to rearrange a list of 1245 numbers from 1 to 1245 in a random order - even though there is no "0" in the list, the random number generator keeps giving me one. Otherwise it looks alright except that there is also a 1245 and so some number in the middle is being lost. Can anyone see a problem with this code, as I can't. I am a relatively new programmer using g++ under Linux. Thanks in advance.
Heres the code (the header files are from the main program):

#include <iostream>
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

int seed, a, j, np = 1245, index;

double randomNumber, ordered[1245], ordered_store[1245], random_order[1245];

void random_number_generator()

{
  double ran;
  srand (seed);
  ran = rand();
  randomNumber = ran / RAND_MAX;
  seed++;
}

void random_sequence()

{
  double temp;
  for (a = 1; a <= np; a++)
    {
      ordered[a - 1] = a;
      ordered_store[a - 1] = a;
      random_order[a - 1] = 0;
    }
  int range = int(np);
  for (a = 0; a < np; a++)
    {
      random_number_generator();
      temp = randomNumber * range;
      index = int(temp) + 1;
      random_order[a] = ordered[index];
      for (j = index; j < np - a; j++)
        {
          ordered[j] = ordered[j + 1];
        }
      range--;
    }
}

void test_3()
{
  fstream fileout ("random_order", ios::out);
  for (a = 0; a < np; a++)
    {
      fileout << " " << random_order[a] << "   " << ordered_store[a] << endl;
    }
}

main()
{
  random_sequence();
  test_3();
  return 0;
}

0
Comment
Question by:bobhope021500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2534493
always add 1 to the result of the random number
0
 

Author Comment

by:bobhope021500
ID: 2534494
Edited text of question.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2534521
Please do not edit the text of the question, rather use comments. Changing the question text may produce strange looking comment threads. Thank you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2534545
You should not keep seeding the random number generator.  Just seed 1 time, at the start of the program.

the random_number_generator() function should return a random number rather than placing the result in a global varaible.  Using a global variable in this way is a very bad idea.  It can lead to many problems and can make a program hard to debug.  whenever possible, return information through a return value, or perhaps through a reference parameter, not through "invisible" means.

The line
  randomNumber = ran / RAND_MAX;

will generate a number between 0 and 1 inclusive.  (0 and 1 are possible).

Byt then you convert that number bacj to an integer.  Why not just keep it an integer?


for (a = 0; a < np; a++)
{
   index = rand() % range;
  // index is now from 0 to range -1  (is it supposed to go to range?  I'm not sure.  If so mode by range+1)
   random_order[a] = ordered[index];
}
0
 

Author Comment

by:bobhope021500
ID: 2534605
Thanks, that sorted it out. I included the seed incrementation in order to increase the element of randomness - I am developing neural network software which relies on the random order of the input data to emphasise the characteristics of the data rather than the way it is presented. But I'm sure this won't be a problem and if it is I may be back! Thank you.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2534974
>> I included the seed incrementation in order to
>> increase the element of randomness
But it doesn't.  You are seeding with sequential numbers, that is no more random than not reseeding.  I fact it may be far less random (It may cause sequences of repeated numbers).  Your best bet is to seed with a single psuedo-random number one time, like the current time.  Then let the sequence proceed unaltered (un-reseeded) from there.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 126
Need some help with mailto 16 41
using interface in TLB 3 26
Creating a Windows C++ program in visual studio 2015 5 29
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question